Skip to content
Open
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
10 changes: 9 additions & 1 deletion src/openai/lib/_parsing/_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,15 @@ def maybe_parse_content(
message: ChatCompletionMessage | ParsedChatCompletionMessage[object],
) -> ResponseFormatT | None:
if has_rich_response_format(response_format) and message.content and not message.refusal:
return _parse_content(response_format, message.content)
try:
return _parse_content(response_format, message.content)
except pydantic.ValidationError:
log.warning(
"Failed to parse structured output content into %s; "
"`.parsed` will be `None`. Raw content is available via `.content`.",
response_format.__name__ if hasattr(response_format, "__name__") else response_format,
)
return None

return None

Expand Down
38 changes: 38 additions & 0 deletions tests/lib/chat/test_completions.py
Original file line number Diff line number Diff line change
Expand Up @@ -993,3 +993,41 @@ def test_parse_method_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpe
checking_client.chat.completions.parse,
exclude_params={"response_format", "stream"},
)


@pytest.mark.respx(base_url=base_url)
def test_parse_invalid_json_returns_none(
client: OpenAI, respx_mock: MockRouter, monkeypatch: pytest.MonkeyPatch
) -> None:
"""When the API returns non-JSON content (e.g. whitespace), `parsed` should be None
rather than raising an unhandled ValidationError.

Regression test for https://github.com/openai/openai-python/issues/1763
"""

class Location(BaseModel):
city: str
temperature: float
units: Literal["c", "f"]

completion = make_snapshot_request(
lambda c: c.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{
"role": "user",
"content": "What's the weather like in SF?",
},
],
response_format=Location,
),
content_snapshot=snapshot(
'{"id": "chatcmpl-invalid-json", "object": "chat.completion", "created": 1727346143, "model": "gpt-4o-2024-08-06", "choices": [{"index": 0, "message": {"role": "assistant", "content": "\\n \\n\\n \\n \\n", "refusal": null}, "logprobs": null, "finish_reason": "stop"}], "usage": {"prompt_tokens": 79, "completion_tokens": 14, "total_tokens": 93, "completion_tokens_details": {"reasoning_tokens": 0}}, "system_fingerprint": "fp_5050236cbd"}'
),
path="/chat/completions",
mock_client=client,
respx_mock=respx_mock,
)

assert completion.choices[0].message.parsed is None
assert completion.choices[0].message.content == "\n \n\n \n \n"