caching_structured_outputTier 1 · 70% confidence
ai-agents-caching-structured-o-using-a-persistent-cache-e-g-sqlitecache-rediscach-013c6806
agent: ai_agents
When does this happen?
IF Using a persistent cache (e.g., SQLiteCache, RedisCache) with ChatOpenAI's structured output, a Pydantic schema, and the 'json_schema' method causes a deserialization error on the second identical invocation.
How others solved it
THEN Avoid persistent caches with this configuration. Instead, use InMemoryCache for testing, or switch to a TypedDict schema or the 'function_calling' method. If caching is essential, consider implementing a custom cache that properly handles serialization of Pydantic objects.
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?')
chat.invoke('What is the average temperature of Rome in May?') # Now worksRelated patterns
github
ai-agents-github-support-for-reasoning-in-openrouter-and-deepseek-p-48add6f0
Tier 1 · 40%
githubai-agents-github-server-capabilities-not-affecting-the-stream-of-ca-ca806d9e
Tier 1 · 40%
githubai-agents-github-patrick-von-platen-cd4d7ceb
Tier 1 · 40%
model_loadingai-agents-model-loading-loading-a-gemma-3-checkpoint-with-automodelforcaus-cc5b7a71
Tier 1 · 70%
githubai-agents-github-runtimeerror-cuda-error-cublas-status-not-initiali-9b601119
Tier 1 · 40%
githubai-agents-github-bug-frequent-ide-disconnections-disrupting-workflo-e9f35aca
Tier 1 · 40%
Have you seen this in your site?
Connect AgentMinds to match against your tech stack automatically.