Skip to content

Fix GoogleModelSettings.google_cached_content so request omits system_instruction, tools, and tool_config#5681

Merged
dsfaccini merged 10 commits into
pydantic:mainfrom
gaurav0107:fix/5671-googlemodelsettings-google-cached-conten
Jun 2, 2026
Merged

Fix GoogleModelSettings.google_cached_content so request omits system_instruction, tools, and tool_config#5681
dsfaccini merged 10 commits into
pydantic:mainfrom
gaurav0107:fix/5671-googlemodelsettings-google-cached-conten

Conversation

@gaurav0107
Copy link
Copy Markdown
Contributor

Summary

When GoogleModelSettings.google_cached_content is set, the outgoing
GenerateContentConfig no longer includes system_instruction, tools,
or tool_config. The cache resource owns those fields; both the Gemini
API and Vertex AI reject requests that supply them alongside
cached_content with 400 INVALID_ARGUMENT:

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

Before this change, GoogleModelSettings.google_cached_content was
unusable in any realistic agent: as soon as the agent had a tool
registered or produced a non-None system_instruction, every request
would 400.

Changes

  • pydantic_ai_slim/pydantic_ai/models/google.py::_build_content_and_config:
    set system_instruction, tools, and tool_config to None when
    google_cached_content is set; keep the existing shape otherwise.

  • Tightened the docstring on GoogleModelSettings.google_cached_content
    to spell out the omission semantics.

  • Added regression tests in tests/models/test_google.py covering the
    cache-set, cache-unset, and trivial paths via
    mocker.patch.object(model.client.aio.models, 'generate_content', ...),
    mirroring the existing test_google_top_k_propagation pattern.

  • Closes GoogleModelSettings.google_cached_content unusable: request still includes system_instruction/tools, Vertex 400s #5671

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.

…google_cached_content` is set

Vertex AI and the Gemini API reject requests that combine `cached_content`
with `system_instruction`, `tools`, or `tool_config` (`400 INVALID_ARGUMENT`:
"Tool config, tools and system instruction should not be set in the request
when using cached content."). The cache resource itself owns those fields.

Before this change, `GoogleModelSettings.google_cached_content` was unusable
in any realistic agent: as soon as the agent had a tool registered or
produced a non-`None` `system_instruction`, every request would 400.

Also tightens the docstring on `GoogleModelSettings.google_cached_content`
to spell out the omission semantics, and adds three regression tests in
`tests/models/test_google.py` covering the cache-set, cache-unset, and
trivial paths via `mocker.patch.object(model.client.aio.models, 'generate_content', ...)`.

Closes pydantic#5671
@github-actions github-actions Bot added size: S Small PR (≤100 weighted lines) bug Report that something isn't working, or PR implementing a fix labels May 27, 2026
@gaurav0107 gaurav0107 marked this pull request as ready for review May 27, 2026 05:12
dsfaccini added 2 commits May 27, 2026 19:21
…ing case

Collapses the three near-identical mocker-based tests for the
`google_cached_content` omission fix into one parametrized test (3 cases ×
2 `stream` values). The new `stream=True` axis exercises
`generate_content_stream`, which shares `_build_content_and_config` with
the non-streaming path — previously untested. Addresses review-tests item
(a) on PR pydantic#5681.
…r tools

Adds a `UserWarning` from `_build_content_and_config` whenever
`google_cached_content` is set and either `system_instruction` or `tools`
(and thus `tool_config`) would otherwise have been sent — the cache
resource owns those fields, so they're stripped silently otherwise. The
parametrized cache-content test gains an `expect_warning` axis covering
all three cases. Also documents the setting and the silent-ignore caveat
in `docs/models/google.md`. Addresses review items (b) and (d) on PR
pydantic#5681.

A VCR cassette exercising a real `cachedContents` resource (item c) is
skipped — needs provisioning of a real cache via `client.caches.create()`
with ≥4096 input tokens and a TTL, which is beyond simple cassette
recording.
@github-actions github-actions Bot added size: M Medium PR (101-500 weighted lines) and removed size: S Small PR (≤100 weighted lines) labels May 28, 2026
github-actions[bot]

This comment was marked as resolved.

github-actions[bot]

This comment was marked as resolved.

github-actions[bot]

This comment was marked as resolved.

dsfaccini added 2 commits May 27, 2026 21:02
…rning

Matches the formatting of neighbouring warnings in the file
(`_get_deprecated_google_service_tier` etc.) — produces e.g. ``system_instruction`, `tools`, `tool_config`` instead of a bare Python
list repr. Addresses auto-review thread on PR pydantic#5681.
@github-actions github-actions Bot added size: S Small PR (≤100 weighted lines) and removed size: M Medium PR (101-500 weighted lines) labels May 28, 2026
Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

Devin Review found 1 potential issue.

View 2 additional findings in Devin Review.

Open in Devin Review

Comment thread pydantic_ai_slim/pydantic_ai/models/google.py
@github-actions github-actions Bot added size: M Medium PR (101-500 weighted lines) and removed size: S Small PR (≤100 weighted lines) labels May 29, 2026
@dsfaccini dsfaccini merged commit 48ce280 into pydantic:main Jun 2, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: M Medium PR (101-500 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GoogleModelSettings.google_cached_content unusable: request still includes system_instruction/tools, Vertex 400s

2 participants