Skip to content

Commit 29cec0d

Browse files
Oxygen56TaoChenOSU
andauthored
Python: fix: use getattr for non-OpenAI provider response compatibility (microsoft#6270)
* fix: use getattr for non-OpenAI provider response compatibility Fixes microsoft#6234 Fixes microsoft#6235 Use getattr with None fallback for system_fingerprint and output attributes to prevent AttributeError when non-OpenAI providers return response objects without these fields. * fix: use typed variable for response output to satisfy pyright Fixes microsoft#6235 Use getattr with None fallback for the output attribute, and assign to a typed list variable before the match statement to help pyright narrow the response item types correctly. * fix: rename response_outputs to avoid name collision with case-block variable Fixes microsoft#6235 Rename outputs to response_outputs on line 1974 to avoid mypy error about conflicting variable names in the match statement's case blocks. Also use list[Any] for explicit generic type annotation. * fix: use cast(list[Any]) for response output to satisfy pyright Fixes microsoft#6235 The getattr() call returns Unknown type which pyright cannot narrow in the match statement. Use an explicit cast to list[Any]. * fix: use hasattr guard instead of getattr for response.output Fixes microsoft#6235 Using hasattr(response, 'output') and then accessing response.output directly gives pyright enough type information to verify the match statement exhaustiveness. This avoids the cast(list[Any]) approach which pyright still flagged as partially unknown. * fix: use ternary operator for response_outputs assignment Replace if-else block with ternary expression to satisfy ruff SIM108 lint rule. This fixes the Package Checks (3.11) CI failure. * fix: use ternary with cast for ruff SIM108 and pyright type safety Replace if-else block with ternary expression using cast(list[Any], ...) to satisfy: - ruff SIM108 (use ternary instead of if-else) - ruff E501 (line length < 120) - pyright type narrowing (cast preserves type info lost in ternary) All local checks pass: ruff check, ruff format, pyright, 298 tests. * fix: replace hasattr+cast with try/except to preserve pyright types --------- Co-authored-by: Tao Chen <[email protected]>
1 parent 96d242f commit 29cec0d

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎python/packages/openai/agent_framework_openai/_chat_client.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,11 @@ def _parse_response_from_openai(
19971997
metadata: dict[str, Any] = response.metadata or {}
19981998
contents: list[Content] = []
19991999
local_shell_tool_name = self._get_local_shell_tool_name(options.get("tools"))
2000-
for item in response.output: # type: ignore[reportUnknownMemberType]
2000+
try:
2001+
response_outputs = response.output # type: ignore[reportUnknownMemberType]
2002+
except AttributeError:
2003+
response_outputs = []
2004+
for item in response_outputs: # type: ignore[reportUnknownVariableType]
20012005
match item.type:
20022006
# types:
20032007
# ParsedResponseOutputMessage[Unknown] |

‎python/packages/openai/agent_framework_openai/_chat_completion_client.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,13 +788,13 @@ def _parse_text_from_openai(self, choice: Choice | ChunkChoice) -> Content | Non
788788
def _get_metadata_from_chat_response(self, response: ChatCompletion) -> dict[str, Any]:
789789
"""Get metadata from a chat response."""
790790
return {
791-
"system_fingerprint": response.system_fingerprint,
791+
"system_fingerprint": getattr(response, "system_fingerprint", None),
792792
}
793793

794794
def _get_metadata_from_streaming_chat_response(self, response: ChatCompletionChunk) -> dict[str, Any]:
795795
"""Get metadata from a streaming chat response."""
796796
return {
797-
"system_fingerprint": response.system_fingerprint,
797+
"system_fingerprint": getattr(response, "system_fingerprint", None),
798798
}
799799

800800
def _get_metadata_from_chat_choice(self, choice: Choice | ChunkChoice) -> dict[str, Any]:

0 commit comments

Comments
 (0)