Skip to content

fix(py): stop Gemini tool-calling loops and log failed GenkitError turns - #107

Merged
huangjeff5 merged 2 commits into
mainfrom
fix/py-day1-tools-schema-and-failed-turn-log
Nov 12, 2026
Merged

huangjeff5 merged 2 commits into
mainfrom
fix/py-day1-tools-schema-and-failed-turn-log

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

ai.generate() with tools + output_schema or tool_choice='required' no longer loops 50 turns or fails with 400/504, and failed turns now log one error line to stderr instead of exiting silently. Without tools, every Gemini and Gemma text model continues to use native wire response_json_schema.

# 1. output_schema WITHOUT tools: native response_json_schema on ALL Gemini & Gemma models
res = await ai.generate(
    model=GoogleAI.gemini('gemini-flash-latest'),
    prompt='Suggest a dish.',
    output_schema=Recipe,
)
# => native response_json_schema (no prompt injection on any model)

# 2. output_schema WITH tools on supporting models (gemini-4-carbon, gemini-4-argon,
#    gemini-3.1-*, gemini-pro-latest, gemini-3-flash-preview, gemini-3.5/3.6-*)
res = await ai.generate(
    model='googleai/gemini-4-carbon',
    prompt='Suggest a bargain dish.',
    tools=[get_sale_items],
    output_schema=Recipe,
)
# => native response_json_schema + tools on the wire -> Recipe(...)

# 3. output_schema WITH tools on models that 400 or loop on native JSON + tools
#    (gemini-2.5-*, gemini-3.7/3.8-flash, gemini-flash-latest, gemma-*)
res = await ai.generate(
    model=GoogleAI.gemini('gemini-flash-latest'),
    prompt='Suggest a bargain dish.',
    tools=[get_sale_items],
    output_schema=Recipe,
)
# before: loops get_sale_items() 50x until FinishReason.ABORTED (or 400 on gemini-2.5-*)
# after:  falls back to prompt-injected schema for this call -> calls tool once -> Recipe(...)

# 4. tool_choice='required' forces the initial tool call, then relaxes so the model can answer
res = await ai.generate(
    model=GoogleAI.gemini('gemini-3.1-pro-preview'),
    prompt='Check sesame stock and return an AllergenReport.',
    tools=[check_inventory],
    tool_choice='required',
    output_schema=AllergenReport,
)
# before: sends mode='ANY' on every turn (or 400s alongside JSON mode); never answers
# after:  Turn 1 sends mode='ANY'; Turn 2 relaxes to mode='AUTO' and returns AllergenReport(...)

Main Changes

  • Selective prompt-injection fallback only when tools + output_schema are paired on incompatible models: Without tools, every Gemini and Gemma text model uses native response_json_schema. When tools are attached alongside output_schema:
    • Supporting models keep ConstrainedSupport.ALL (native response_json_schema + tools): gemini-4-carbon, gemini-4-argon, gemini-3.1-*, gemini-pro-latest, gemini-3-flash-preview, gemini-3.5-*, gemini-3.6-flash, gemini-flash-lite-latest, Vertex tuned endpoints, and any new gemini-4-*+ IDs.
    • Incompatible checkpoints use ConstrainedSupport.NO_TOOLS (native JSON without tools; prompt-injected schema only when tools are passed): gemini-2.5-* (which HTTP-400s on tools + JSON mode), gemini-3.7/3.8-flash / gemini-flash-latest (which loop tool calls or 504), and gemma-*. Passing output_constrained=False forces the same prompt-injection fallback per call on any model.
  • tool_choice='required' relaxes from ANY to AUTO after a tool answers: Gemini's ANY mode forbids text replies and HTTP-400s next to response_mime_type='application/json'. Turn 1 sends ANY without JSON mode to guarantee the tool call; once a tool response arrives, follow-up turns relax to AUTO so the model can emit its final answer.
  • Failed turns log one line to stderr: Non-public GenkitErrors boxed onto res.error (bad key, missing key, unknown model ID) previously logged nothing, so print(res.text) printed an empty line and exited 0. log_dropped_failure now logs one line (no traceback, no prompt text) for every non-PublicError failure.

Breaking changes

None.

@huangjeff5
huangjeff5 added this pull request to stack #108 November 11, 2026 21:34
@huangjeff5
huangjeff5 force-pushed the fix/py-day1-tools-schema-and-failed-turn-log branch 2 times, most recently from 380c040 to fa133c7 Compare November 11, 2026 21:58
Base automatically changed from fix/py-log-failed-turn-one-line to main November 11, 2026 22:39
@huangjeff5
huangjeff5 force-pushed the fix/py-day1-tools-schema-and-failed-turn-log branch from fa133c7 to 3bda1a5 Compare November 11, 2026 22:39
@huangjeff5 huangjeff5 changed the title fix(py): stop Gemini tools+schema loop and log failed GenkitError turns fix(py): stop Gemini tool-calling loops and log failed GenkitError turns Nov 11, 2026
huangjeff5 added a commit that referenced this pull request Nov 11, 2026
@huangjeff5
huangjeff5 force-pushed the fix/py-day1-tools-schema-and-failed-turn-log branch from 3bda1a5 to 7cf32c6 Compare November 11, 2026 22:43
@huangjeff5
huangjeff5 merged commit adf10ed into main Nov 12, 2026
21 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-day1-tools-schema-and-failed-turn-log branch November 12, 2026 02:26
huangjeff5 added a commit that referenced this pull request Nov 12, 2026
…uickstart

Keeps main's GEMINI_FLASH_LATEST (no constrained output with tools, #107)
over this branch's copy of 3.5 Flash supports, and moves the unknown-model
fallback test case off gemini-3.5-flash-lite, which this branch catalogs.
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