-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: fix: handle string response in _get_metadata_from_chat_response (Closes #6234) #6257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -684,6 +684,13 @@ def _prepare_options(self, messages: Sequence[Message], options: Mapping[str, An | |
|
|
||
| def _parse_response_from_openai(self, response: ChatCompletion, options: Mapping[str, Any]) -> ChatResponse: | ||
| """Parse a response from OpenAI into a ChatResponse.""" | ||
| if isinstance(response, str): | ||
| raise ChatClientException( | ||
| maybe_append_azure_endpoint_guidance( | ||
| f"{type(self)} service returned an invalid response (str instead of ChatCompletion): {response!r}", | ||
|
|
||
| azure_endpoint=self.azure_endpoint, | ||
| ), | ||
| ) | ||
|
Comment on lines
+687
to
+693
|
||
| response_metadata = self._get_metadata_from_chat_response(response) | ||
| messages: list[Message] = [] | ||
| finish_reason: FinishReason | None = None | ||
|
|
@@ -788,7 +795,7 @@ def _parse_text_from_openai(self, choice: Choice | ChunkChoice) -> Content | Non | |
| def _get_metadata_from_chat_response(self, response: ChatCompletion) -> dict[str, Any]: | ||
| """Get metadata from a chat response.""" | ||
| return { | ||
| "system_fingerprint": response.system_fingerprint, | ||
| "system_fingerprint": getattr(response, "system_fingerprint", None), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we mirror this onto the streaming path too? The fix hardens non-streaming, but here |
||
| } | ||
|
|
||
| def _get_metadata_from_streaming_chat_response(self, response: ChatCompletionChunk) -> dict[str, Any]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we covering the new branch? Couldn't find a test exercising the str -> ChatClientException path in
python/packages/openai/tests/openai/test_openai_chat_completion_client.py. Existing direct-call tests make it nearly free:client._parse_response_from_openai("some string", {})insidepytest.raises(ChatClientException), asserting substring"str instead of ChatCompletion"rather than the full message (Azure suffix is env-dependent). Pins the contract so a refactor can't quietly regress it back to the AttributeError.