Skip to content

fix(openai): avoid PydanticSerializationUnexpectedValue for structured output - #35543

Merged
ccurme (ccurme) merged 1 commit into
langchain-ai:masterfrom
JasonCZMeng:fix/issue-35538
Mar 5, 2026
Merged

fix(openai): avoid PydanticSerializationUnexpectedValue for structured output#35543
ccurme (ccurme) merged 1 commit into
langchain-ai:masterfrom
JasonCZMeng:fix/issue-35538

Conversation

@JasonCZMeng

Copy link
Copy Markdown
Contributor

Summary

Exclude the parsed field from model_dump() in _create_chat_result() to prevent PydanticSerializationUnexpectedValue warnings when using structured output.

Fixes #35538

Root Cause

When OpenAI returns a parsed completion, message.parsed holds an arbitrary Pydantic BaseModel instance (the user's structured output schema). The existing code calls response.model_dump() which tries to serialize this into a dict — but the OpenAI SDK's ParsedChatCompletion types the parsed field as None, so Pydantic emits a serialization warning when it encounters a real model there.

Fix

Exclude parsed from the model_dump() call since it's already copied separately into additional_kwargs["parsed"] from the typed response object (at ~L1602). No data is lost.

Changes

libs/partners/openai/langchain_openai/chat_models/base.py

  • Added exclude={"choices": {"__all__": {"message": {"parsed"}}}} to the model_dump() call in _create_chat_result()

libs/partners/openai/tests/unit_tests/chat_models/test_base.py

  • Added test_create_chat_result_avoids_parsed_model_dump_warning() — mocks an OpenAI completion with a BaseModel in parsed and asserts no serialization warning is emitted

…d output

Exclude the 'parsed' field from model_dump() in _create_chat_result()
since it may hold arbitrary Pydantic BaseModel instances from structured
output. The parsed value is already copied separately from the typed
response object into additional_kwargs.

Fixes langchain-ai#35538
@github-actions github-actions Bot added external integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs fix For PRs that implement a fix and removed external labels Mar 3, 2026
@codspeed-hq

codspeed-hq Bot commented Mar 3, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 6 untouched benchmarks
⏩ 30 skipped benchmarks1


Comparing JasonCZMeng:fix/issue-35538 (9bbea53) with master (e50625e)

Open in CodSpeed

Footnotes

  1. 30 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ccurme
ccurme (ccurme) merged commit f698b43 into langchain-ai:master Mar 5, 2026
81 checks passed
Kesav Kumar Jayakumar (Kesav2k04) added a commit to Kesav2k04/langchain that referenced this pull request Jul 2, 2026
…ctured output

Applies the same parsed exclusion logic from langchain-ai#35543 to the streaming delta payload, preventing noisy UserWarning logs during with_structured_output streaming.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix For PRs that implement a fix integration PR made that is related to a provider partner package integration openai `langchain-openai` package issues & PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PydanticSerializationUnexpectedValue warning when using structured output

2 participants