Skip to content

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

Description

@lskrajny-marcura

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions