fix(openai): avoid PydanticSerializationUnexpectedValue for structured output - #35543
Merged
Merged
Conversation
…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
Jason Meng (JasonCZMeng)
requested review from
ccurme (ccurme) and
Mason Daugherty (mdrxy)
as code owners
March 3, 2026 22:51
Merging this PR will not alter performance
Comparing Footnotes
|
ccurme (ccurme)
approved these changes
Mar 5, 2026
29 tasks
29 tasks
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.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Exclude the
parsedfield frommodel_dump()in_create_chat_result()to preventPydanticSerializationUnexpectedValuewarnings when using structured output.Fixes #35538
Root Cause
When OpenAI returns a parsed completion,
message.parsedholds an arbitrary PydanticBaseModelinstance (the user's structured output schema). The existing code callsresponse.model_dump()which tries to serialize this into a dict — but the OpenAI SDK'sParsedChatCompletiontypes theparsedfield asNone, so Pydantic emits a serialization warning when it encounters a real model there.Fix
Exclude
parsedfrom themodel_dump()call since it's already copied separately intoadditional_kwargs["parsed"]from the typed response object (at ~L1602). No data is lost.Changes
libs/partners/openai/langchain_openai/chat_models/base.pyexclude={"choices": {"__all__": {"message": {"parsed"}}}}to themodel_dump()call in_create_chat_result()libs/partners/openai/tests/unit_tests/chat_models/test_base.pytest_create_chat_result_avoids_parsed_model_dump_warning()— mocks an OpenAI completion with a BaseModel inparsedand asserts no serialization warning is emitted