langchain - ✅(Solved) Fix PydanticSerializationUnexpectedValue warning when using structured output [3 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
langchain-ai/langchain#35538Fetched 2026-04-08 00:25:48
View on GitHub
Comments
1
Participants
2
Timeline
11
Reactions
0
Timeline (top)
labeled ×4closed ×1commented ×1cross-referenced ×1

using structured output produces a serializer warning. This behavior seems to have been introduced with one the recent langchain releases.

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

using structured output produces a serializer warning. This behavior seems to have been introduced with one the recent langchain releases.

Fix Action

Fix / Workaround

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Other Dependencies

aiohttp: 3.13.3 dataclasses-json: 0.6.7 httpx: 0.28.1 httpx-sse: 0.4.3 jsonpatch: 1.33 langgraph: 1.0.10 numpy: 2.4.2 openai: 2.24.0 opentelemetry-api: 1.39.0 opentelemetry-exporter-otlp-proto-http: 1.39.0 opentelemetry-sdk: 1.39.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pydantic-settings: 2.13.1 pytest: 9.0.2 pyyaml: 6.0.3 PyYAML: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.3 SQLAlchemy: 2.0.48 sqlalchemy: 2.0.48 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 wrapt: 1.17.3 xxhash: 3.6.0 zstandard: 0.25.0

PR fix notes

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

Description (problem / solution / changelog)

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

Changed files

  • libs/partners/openai/langchain_openai/chat_models/base.py (modified, +7/-1)
  • libs/partners/openai/tests/unit_tests/chat_models/test_base.py (modified, +43/-0)

PR #136: Update for notebooks in python-recipes

Description (problem / solution / changelog)

This PR is a follow-up to previously merged PR #133 — finishing remaining notebook updates to resolve failed CI/CD tests.

Updates in this PR include:

  • python-recipes/RAG/01_redisvl.ipynb - Updated dependencies related to langchain-text-splitters, migrated from the OpenAI Chat Completions API to the OpenAI Responses API, and did some minor refactors to the answer_question function for DRY-ness and to ensure consistent behaviour.

  • python-recipes/RAG/02_langchain.ipynb - Very minor update to the documentation, fixed an outdated link.

  • python-recipes/RAG/04_advanced_redisvl.ipynb - Updated dependencies related to langchain-text-spliiters and migrated from the OpenAI Chat Completions API to the Responses API.

  • python-recipes/RAG/07_user_role_based_rag.ipynb - Updated dependencies related to langchain-text-splitters. Also wrapped some notebook cells in try-except blocks for clarity and to reduce confusion.

  • python-recipes/vector-search/02_hybrid_search.ipynb - Updated versioning for redis-py dependencies and resolved error pertaining to unclear dtypes when using pandas DataFrames.

  • python-recipes/vector-search/05_multivector_search.ipynb - Resolved error with redisvl import and included sentence-transformer import.

  • python-recipes/agents/00_langgraph_redis_agentic_rag.ipynb - Updated dependencies related to langchain-core.tools.retriever and added Literal construct to specify exact outputs for agent.

Changed files

  • python-recipes/RAG/01_redisvl.ipynb (modified, +202/-214)
  • python-recipes/RAG/02_langchain.ipynb (modified, +4/-4)
  • python-recipes/RAG/04_advanced_redisvl.ipynb (modified, +174/-148)
  • python-recipes/RAG/07_user_role_based_rag.ipynb (modified, +136/-388)
  • python-recipes/agents/00_langgraph_redis_agentic_rag.ipynb (modified, +47/-53)
  • python-recipes/vector-search/02_hybrid_search.ipynb (modified, +799/-120)
  • python-recipes/vector-search/05_multivector_search.ipynb (modified, +99/-53)

Code Example

from langchain_openai import ChatOpenAI
from pydantic.main import BaseModel

llm = ChatOpenAI(...) # not using responses API

class ModelOutput(BaseModel):
    output: str


output = llm.with_structured_output(ModelOutput).invoke("What is the capital of France?")

---

PydanticSerializationUnexpectedValue(Expected `none` - serialized value may not be as expected [field_name='parsed', input_value=ModelOutput(output='The c...al of France is Paris.'), input_type=ModelOutput])
  return self.__pydantic_serializer__.to_python(
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug, not a usage question.
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).
  • This is not related to the langchain-community package.
  • I posted a self-contained, minimal, reproducible example. A maintainer can copy it and run it AS IS.

Package (Required)

  • langchain
  • langchain-openai
  • langchain-anthropic
  • langchain-classic
  • langchain-core
  • langchain-model-profiles
  • langchain-tests
  • langchain-text-splitters
  • langchain-chroma
  • langchain-deepseek
  • langchain-exa
  • langchain-fireworks
  • langchain-groq
  • langchain-huggingface
  • langchain-mistralai
  • langchain-nomic
  • langchain-ollama
  • langchain-openrouter
  • langchain-perplexity
  • langchain-qdrant
  • langchain-xai
  • Other / not sure / general

Related Issues / PRs

No response

Reproduction Steps / Example Code (Python)

from langchain_openai import ChatOpenAI
from pydantic.main import BaseModel

llm = ChatOpenAI(...) # not using responses API

class ModelOutput(BaseModel):
    output: str


output = llm.with_structured_output(ModelOutput).invoke("What is the capital of France?")

Error Message and Stack Trace (if applicable)

PydanticSerializationUnexpectedValue(Expected `none` - serialized value may not be as expected [field_name='parsed', input_value=ModelOutput(output='The c...al of France is Paris.'), input_type=ModelOutput])
  return self.__pydantic_serializer__.to_python(

Description

using structured output produces a serializer warning. This behavior seems to have been introduced with one the recent langchain releases.

System Info

System Information

OS: Linux OS Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024 Python Version: 3.14.3 (main, Feb 3 2026, 22:52:18) [Clang 21.1.4 ]

Package Information

langchain_core: 1.2.17 langchain: 1.2.10 langchain_community: 0.4.1 langsmith: 0.7.10 langchain_classic: 1.0.1 langchain_openai: 1.1.10 langchain_text_splitters: 1.1.1 langgraph_sdk: 0.3.9

Optional packages not installed

deepagents deepagents-cli

Other Dependencies

aiohttp: 3.13.3 dataclasses-json: 0.6.7 httpx: 0.28.1 httpx-sse: 0.4.3 jsonpatch: 1.33 langgraph: 1.0.10 numpy: 2.4.2 openai: 2.24.0 opentelemetry-api: 1.39.0 opentelemetry-exporter-otlp-proto-http: 1.39.0 opentelemetry-sdk: 1.39.0 orjson: 3.11.7 packaging: 26.0 pydantic: 2.12.5 pydantic-settings: 2.13.1 pytest: 9.0.2 pyyaml: 6.0.3 PyYAML: 6.0.3 requests: 2.32.5 requests-toolbelt: 1.0.0 rich: 14.3.3 SQLAlchemy: 2.0.48 sqlalchemy: 2.0.48 tenacity: 9.1.4 tiktoken: 0.12.0 typing-extensions: 4.15.0 uuid-utils: 0.14.1 wrapt: 1.17.3 xxhash: 3.6.0 zstandard: 0.25.0

extent analysis

Fix Plan

Problem: PydanticSerializationUnexpectedValue error when using structured output with LangChain

Solution:

  1. Downgrade langchain to version 1.2.9:

pip install langchain==1.2.9

   This should resolve the issue as the error was introduced in version 1.2.10.

2. **Update `langchain_openai` to version 1.1.9**:
   ```bash
pip install langchain_openai==1.1.9

This is to ensure that you have the latest compatible version of langchain_openai.

  1. Verify the fix: Run the reproduction code again and check if the error is resolved.

Verification

  • Run the reproduction code and verify that the output is as expected.
  • Check the console output for any errors or warnings.

Extra Tips

  • Always check the changelog and release notes for the library you're using to see if any breaking changes have been made.
  • Downgrade to a previous version of the library as a last resort, and try to find a more permanent solution.
  • If you're using a library that's still in development, be prepared for breaking changes and plan accordingly.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING