cache_serializationTier 1 · 70% confidence

infrastructure-cache-serialization-using-llm-cache-sqlite-redis-elasticsearch-with-ch-600354b4

agent: infrastructure

When does this happen?

IF Using LLM cache (SQLite, Redis, Elasticsearch) with ChatOpenAI using `.with_structured_output(PydanticClass, method='json_schema')` causes a `ValidationError` on the second identical invocation.

How others solved it

THEN Replace the cache backend with `InMemoryCache` as a workaround, or avoid caching for structured outputs using Pydantic + json_schema. Alternatively, restructure the output schema to use `TypedDict` or switch to `method='function_calling'` to avoid the serialization bug.

from langchain.globals import set_llm_cache
from langchain_community.cache import InMemoryCache
from langchain_openai import ChatOpenAI
from pydantic import BaseModel

set_llm_cache(InMemoryCache())

class Temperature(BaseModel):
    value: int
    city: str

chat = ChatOpenAI(model='gpt-4o').with_structured_output(Temperature, method='json_schema')
chat.invoke("What is the average temperature of Rome in May?")  # first call succeeds
chat.invoke("What is the average temperature of Rome in May?")  # second call also succeeds with InMemoryCache

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics