Skip to content

fix(genai): omit tools and system instruction when cached_content is provided - #2591

Closed
kishansaaai wants to merge 1 commit into
567-labs:mainfrom
kishansaaai:fix-v2-genai-cached-content
Closed

kishansaaai wants to merge 1 commit into
567-labs:mainfrom
kishansaaai:fix-v2-genai-cached-content

Conversation

@kishansaaai

@kishansaaai kishansaaai commented Sep 3, 2026 •

Copy link
Copy Markdown

Summary

In Google GenAI, passing system_instruction, tools, or tool_config in requests when using cached_content is rejected by the Gemini API with:

400 INVALID_ARGUMENT: Tool config, tools and system instruction should not be set in the request when using cached content.

because those parameters must be defined inside the cache itself (per https://ai.google.dev/gemini-api/docs/caching).

In v2 handlers (instructor/v2/providers/genai/handlers.py), GenAIToolsHandler.prepare_request and GenAIStructuredOutputsHandler.prepare_request previously built base_config unconditionally, including system_instruction, tools, and tool_config even when the caller specified cached_content.

This change:

  1. Adds _extract_cached_content on GenAIHandlerBase to detect cached_content from config (as a dict or GenerateContentConfig instance) or top-level kwargs.
  2. Omits system_instruction, tools, and tool_config from base_config in GenAIToolsHandler.prepare_request when cached_content is present.
  3. Omits system_instruction from base_config in GenAIStructuredOutputsHandler.prepare_request when cached_content is present (preserving response_mime_type and response_schema).
  4. Omits system_instruction in _prepare_without_response_model when cached_content is present.
  5. Ensures top-level cached_content kwargs are merged into config in update_genai_kwargs.
  6. Adds unit tests in tests/v2/test_genai_integration.py verifying that tools, tool_config, and system_instruction are omitted when cached_content is supplied in both Mode.TOOLS and Mode.JSON.

Issue ticket number and link

Fixes #2580

Checklist before requesting a review

  • I have performed a self-review of my code
  • If it is a core feature, I have added thorough tests.
  • If it is a core feature, I have added documentation.

@jxnl

jxnl commented Sep 5, 2026

Copy link
Copy Markdown
Collaborator

Superseded by the consolidated implementation in #2597: #2597 . The release branch includes this fix or its deduplicated equivalent, with additional regression coverage. Closing the overlapping source PR to keep review in one place. This does not mean the release is merged or shipped: #2597 remains draft pending exact-head CI, independent review, and unresolved security sign-off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v2 GenAI handlers regression: cached_content requests now include system_instruction/tools/tool_config → 400 INVALID_ARGUMENT

2 participants