Skip to content

fix(genai): Support cached_content for Google context caching#1987

Merged
jxnl merged 2 commits into567-labs:mainfrom
b-antosik-marcura:fix-genai-cache
Jan 8, 2026
Merged

fix(genai): Support cached_content for Google context caching#1987
jxnl merged 2 commits into567-labs:mainfrom
b-antosik-marcura:fix-genai-cache

Conversation

@b-antosik-marcura
Copy link
Copy Markdown
Contributor

@b-antosik-marcura b-antosik-marcura commented Jan 7, 2026

Summary

When using Google's explicit context caching, the API requires that system_instruction, tools, and tool_config are NOT set in the request - they should already be part of the cached content.

This PR fixes cached_content propagation for Google GenAI by:

  • Extracting cached_content from user-provided GenerateContentConfig
  • Skipping system_instruction, tools, tool_config when cached_content is present
  • Adding cached_content to the list of config fields that get merged

Related Issues

Documentation

Test plan

  • Added unit tests for cached_content extraction
  • Added unit tests verifying system_instruction/tools/tool_config are skipped when cached_content is present
  • All existing tests pass
  • Manual test and example summary from LangSmith:
image

Important

Fix handling of cached_content in Google GenAI context caching by skipping certain fields and adding tests.

  • Behavior:
    • In handle_genai_structured_outputs() and handle_genai_tools(), skip setting system_instruction, tools, and tool_config if cached_content is present.
    • Extract cached_content from GenerateContentConfig in update_genai_kwargs() and add it to config_fields_to_merge.
  • Tests:
    • Add tests in test_genai_config_merging.py to verify cached_content extraction and behavior when present.
    • Ensure system_instruction, tools, and tool_config are not set when cached_content is provided.
    • Verify that cached_content does not override existing base config values.

This description was created by Ellipsis for b4de631. You can customize this summary. It will automatically update as commits are pushed.

When using Google's explicit context caching, the API requires that
`system_instruction`, `tools`, and `tool_config` are NOT set in the
request - they should already be part of the cached content.

This PR:
- Extracts `cached_content` from user-provided GenerateContentConfig
- Skips adding `system_instruction`, `tools`, `tool_config` when
  `cached_content` is present
- Adds `cached_content` to the list of config fields that get merged

Fixes issues similar to 567-labs#1759 (labels not preserved)
See: https://ai.google.dev/gemini-api/docs/caching
Copy link
Copy Markdown
Contributor

@ellipsis-dev ellipsis-dev Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to b4de631 in 1 minute and 0 seconds. Click for details.
  • Reviewed 261 lines of code in 2 files
  • Skipped 0 files when reviewing.
  • Skipped posting 5 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. instructor/providers/gemini/utils.py:350
  • Draft comment:
    Good addition: 'cached_content' is now included in the config fields merge. This ensures that any user-provided cached content is propagated correctly.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 85% None
2. instructor/providers/gemini/utils.py:887
  • Draft comment:
    The extraction of 'cached_content' (along with 'thinking_config') from the user config is implemented clearly. Consider refactoring this repeated extraction logic into a helper function to adhere to DRY principles.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 85% None
3. instructor/providers/gemini/utils.py:1000
  • Draft comment:
    The conditional block that omits tools, tool_config, and system_instruction when 'cached_content' is present is well-commented and clear. Consider consolidating similar extraction logic between the structured outputs and tools handlers for maintainability.
  • Reason this comment was not posted:
    Confidence changes required: 33% <= threshold 85% None
4. tests/test_genai_config_merging.py:240
  • Draft comment:
    Test 'test_update_genai_kwargs_config_object_cached_content' effectively verifies that 'cached_content' is extracted from the config object. Good coverage for this new feature.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 85% None
5. tests/test_genai_config_merging.py:285
  • Draft comment:
    The tests for 'handle_genai_structured_outputs' and 'handle_genai_tools' properly check that attributes like system_instruction, tools, and tool_config are omitted when 'cached_content' is provided. This comprehensive test coverage reinforces the intended behavior.
  • Reason this comment was not posted:
    Confidence changes required: 0% <= threshold 85% None

Workflow ID: wflow_22PHFL1GIzlh6bW5

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

@jxnl jxnl merged commit 142be68 into 567-labs:main Jan 8, 2026
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.

2 participants