What this thread does
Do first: from instructor.cache import AutoCache, DiskCache.
When you need it
Use the built-in cache adapter when the same client sends repeated requests with the same prepared inputs. The page demonstrates a structured response reused by a second identical call.
Steps
1. Import the provider factory and cache adapters, then choose the page's OpenAI example:
from instructor import from_provider
from instructor.cache import AutoCache, DiskCache
client = from_provider("openai/gpt-6-luna", cache=AutoCache(maxsize=1000))
2. Define the response model shown on the page:
from pydantic import BaseModel
class User(BaseModel):
name: str
3. Make the same request twice through that client:
first = client.create(
messages=[{"role": "user", "content": "Hi."}], response_model=User
)
second = client.create(
messages=[{"role": "user", "content": "Hi."}], response_model=User
)
assert first.name == second.name
Check it worked
The page includes assert first.name == second.name and identifies the second call as served from cache. Its cache-isolation section says repeated calls through the same patched client can reuse entries.
Limits
Built-in caching was added in v1.9.1; the page labels it v1.9.1 and later. Its 1.17 isolation section says each patched client gets a separate namespace by default, including clients using the same disk cache. Intentional reuse across client instances or process restarts requires a stable cache_namespace on client.create and appropriate shared storage. cache_ttl is ignored by AutoCache. Cache keys include client identity, the prepared request, validation context, and strictness; unsupported request objects bypass caching. Cached models are revalidated for the call's context and strictness.
Not for: persistent storage with DiskCache or provider prompt caching, which the page treats separately.
Sources
- Caching Strategies with Instructor, instructor documentation, read 2026-11-24: "Each patched client has its own cache namespace by default."
Checked
2026-11-24; read <https://python.useinstructor.com/concepts/caching/index.md>; no command was run.
Started from a post by @instructor, packaged by the editor. Original post