Summary
In 1.15.1, the GenAI handlers omitted system_instruction, tools, and tool_config from the request when the caller passed cached_content (the Gemini API rejects requests that set them alongside cached content — they must live in the cache). See handle_genai_tools / handle_genai_structured_outputs in instructor/providers/gemini/utils.py @ 1.15.1, which guard on user_cached_content is None with the comment "Adding them causes 400 INVALID_ARGUMENT".
In 1.16.0, from_genai routes to the v2 handlers (instructor/v2/providers/genai/handlers.py), and both GenAIToolsHandler.prepare_request and GenAIStructuredOutputsHandler.prepare_request build base_config unconditionally — the cached-content guard is gone. Every structured call that passes config={"cached_content": ...} now fails with:
400 INVALID_ARGUMENT: Tool config, tools and system instruction should not be set in the request when using cached content.
Repro (no network needed — inspect the prepared request)
from instructor.mode import Mode
from instructor.utils.providers import Provider
from instructor.v2.core.registry import mode_registry
import instructor.v2.providers.genai.handlers # noqa: F401
from pydantic import BaseModel
class Out(BaseModel):
x: int
for mode in (Mode.JSON, Mode.TOOLS):
prepare = mode_registry.get_handler(Provider.GENAI, mode)
_, prepared = prepare(Out, {
"messages": [{"role": "user", "content": "hi"}],
"config": {"cached_content": "cachedContents/abc"},
})
cfg = prepared["config"]
print(mode, {
"system_instruction": cfg.system_instruction is not None,
"tools": cfg.tools is not None,
"tool_config": cfg.tool_config is not None,
"cached_content": cfg.cached_content,
})
1.16.0 output: JSON mode sets system_instruction (instructor's own JSON instruction); TOOLS mode sets system_instruction, tools, and tool_config — all alongside cached_content. Sending any of them with cached content is a guaranteed 400 from the Gemini API.
Expected
When cached_content is present in the user config, prepare_request should omit system_instruction, tools, and tool_config, as 1.15.1 did (https://ai.google.dev/gemini-api/docs/caching).
Environment
- instructor 1.16.0, google-genai 1.75.0, Python 3.12
Summary
In 1.15.1, the GenAI handlers omitted
system_instruction,tools, andtool_configfrom the request when the caller passedcached_content(the Gemini API rejects requests that set them alongside cached content — they must live in the cache). Seehandle_genai_tools/handle_genai_structured_outputsininstructor/providers/gemini/utils.py@ 1.15.1, which guard onuser_cached_content is Nonewith the comment "Adding them causes 400 INVALID_ARGUMENT".In 1.16.0,
from_genairoutes to the v2 handlers (instructor/v2/providers/genai/handlers.py), and bothGenAIToolsHandler.prepare_requestandGenAIStructuredOutputsHandler.prepare_requestbuildbase_configunconditionally — the cached-content guard is gone. Every structured call that passesconfig={"cached_content": ...}now fails with:Repro (no network needed — inspect the prepared request)
1.16.0 output: JSON mode sets
system_instruction(instructor's own JSON instruction); TOOLS mode setssystem_instruction,tools, andtool_config— all alongsidecached_content. Sending any of them with cached content is a guaranteed 400 from the Gemini API.Expected
When
cached_contentis present in the user config,prepare_requestshould omitsystem_instruction,tools, andtool_config, as 1.15.1 did (https://ai.google.dev/gemini-api/docs/caching).Environment