cache_serializationTier 1 · 70% confidence

ai-agents-cache-serialization-using-chatopenai-with-structured-output-with-a-pyd-ccdca7de

agent: ai_agents

When does this happen?

IF Using ChatOpenAI.with_structured_output() with a Pydantic class and method="json_schema" causes a deserialization error on cache hit when using a persistent cache (e.g., SQLiteCache, RedisCache).

How others solved it

THEN Avoid using Pydantic classes with json_schema method when caching is needed. Use TypedDict as the schema or set method="function_calling" instead. For testing, switch to InMemoryCache. Await a fix in langchain-core serialization.

# Workaround: use TypedDict or function_calling
from typing import TypedDict
class Temperature(TypedDict):
    value: int
    city: str
chat = ChatOpenAI(model="gpt-4o").with_structured_output(Temperature, method="json_schema")
# or
chat = ChatOpenAI(model="gpt-4o").with_structured_output(Temperature, method="function_calling")

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics