Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,18 @@ 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 not hasattr(response, "choices"):
Comment on lines 685 to +687
preview = repr(response)
if len(preview) > 500:
preview = f"{preview[:497]}..."
Comment on lines +688 to +690
raise ChatClientException(
maybe_append_azure_endpoint_guidance(
f"{type(self)} service returned an invalid OpenAI Chat Completions API response: "
f"expected an object with 'choices', got {type(response).__name__}: {preview}",
azure_endpoint=self.azure_endpoint,
)
)
Comment on lines +691 to +697

response_metadata = self._get_metadata_from_chat_response(response)
messages: list[Message] = []
finish_reason: FinishReason | None = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ def test_get_response_is_defined_on_openai_class() -> None:
assert all(parameter.kind != inspect.Parameter.VAR_KEYWORD for parameter in signature.parameters.values())


def test_parse_response_rejects_non_chat_completion_object() -> None:
client = OpenAIChatCompletionClient(model="test-model", api_key="test-key")
response: Any = "plain backend error"

with pytest.raises(ChatClientException) as exc_info:
client._parse_response_from_openai(response, options={})

exception_message = str(exc_info.value)
assert "invalid OpenAI Chat Completions API response" in exception_message
assert "expected an object with 'choices'" in exception_message
assert "got str" in exception_message
assert "plain backend error" in exception_message
assert "'str' object has no attribute 'system_fingerprint'" not in exception_message
Comment on lines +71 to +76


def test_init_uses_explicit_parameters() -> None:
signature = inspect.signature(RawOpenAIChatCompletionClient.__init__)

Expand Down