Skip to content

fix(gemini): support structured output with tools on Gemini 3 models#4184

Merged
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/debug-agent-response
Apr 15, 2026
Merged

fix(gemini): support structured output with tools on Gemini 3 models#4184
waleedlatif1 merged 4 commits intostagingfrom
waleedlatif1/debug-agent-response

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Gemini 3 models support responseJsonSchema alongside tools/function declarations in the same request — Gemini 2 does not
  • When an Agent block uses a Gemini 3 model with both tools and a response format, structured output was silently skipped, returning plain text instead of JSON
  • Added isGemini3Model() helper and branching logic to use responseJsonSchema (not responseSchema) for Gemini 3 + tools, preserving existing Gemini 2 behavior
  • Verified all scenarios against the live Gemini API: Gemini 3 + tools + schema, Gemini 3 no tools, Gemini 2 + tools (400 rejection), Gemini 2 no tools

Type of Change

  • Bug fix

Testing

Tested manually against the live Gemini API with comprehensive matrix covering all model/tools/schema combinations.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

TheodoreSpeaks and others added 2 commits April 15, 2026 12:06
* feat(posthog): Add tracking on mothership abort (#4023)

Co-authored-by: Theodore Li <theo@sim.ai>

* fix(login): fix captcha headers for manual login  (#4025)

* fix(signup): fix turnstile key loading

* fix(login): fix captcha header passing

* Catch user already exists, remove login form captcha
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 15, 2026 7:12pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 15, 2026

PR Summary

Medium Risk
Changes request/iteration config for Gemini structured outputs and tool loops; incorrect branching could alter response formatting or cause API rejections for some model/tool combinations.

Overview
Fixes structured JSON output when using Gemini 3 models with both tools and responseFormat by sending the schema via responseJsonSchema (while keeping Gemini 2’s behavior of applying responseSchema only when tools are absent or after tool execution).

Adds isGemini3Model() and threads model into the tool-iteration config builder to avoid reapplying/overwriting schema settings when stripping tools for the final response, and updates warnings/logging to distinguish Gemini 2 vs 3 behavior.

Reviewed by Cursor Bugbot for commit 8e93c05. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 15, 2026

Greptile Summary

This PR fixes structured output being silently skipped when using Gemini 3 models with tools by branching on isGemini3Model() to use responseJsonSchema (which Gemini 3 accepts alongside function declarations) instead of the responseSchema path that only works without tools. The prefix-based isGemini3Model helper is future-proof and already accounts for vertex/ prefixed variants.

Confidence Score: 5/5

Safe to merge — targeted bug fix with correct branching logic and no regressions to Gemini 2 behavior.

All findings are P2 or lower. The implementation correctly uses responseJsonSchema for Gemini 3 + tools, preserves it through the tool execution loop via baseConfig inheritance, and leaves Gemini 2 paths entirely unchanged. No P0/P1 issues found.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/providers/utils.ts Adds isGemini3Model() using a prefix check that handles vertex/ variants; clean and future-proof implementation.
apps/sim/providers/gemini/core.ts Branches Gemini 3 to use responseJsonSchema alongside tools in initial config and preserves it through buildNextConfig and streaming paths; Gemini 2 behavior unchanged.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Request: responseFormat + tools?] --> B{tools present?}
    B -- No --> C[responseSchema via cleanSchemaForGemini\nresponseMimeType = application/json]
    B -- Yes --> D{isGemini3Model?}
    D -- Yes --> E[responseJsonSchema = raw schema\nresponseMimeType = application/json\ntools kept in config]
    D -- No --> F[Warn: structured output deferred\ntools kept in config]
    E --> G[Tool execution loop]
    F --> G
    G --> H{More tool calls?}
    H -- Yes --> G
    H -- No --> I{isGemini3Model?}
    I -- Yes --> J[Strip tools\nresponseJsonSchema inherited from baseConfig]
    I -- No --> K[Strip tools\nApply responseSchema + responseMimeType]
    J --> L[Final structured response]
    K --> L
Loading

Reviews (2): Last reviewed commit: "refactor(gemini): use prefix-based Gemin..." | Re-trigger Greptile

Comment thread apps/sim/providers/utils.ts Outdated
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 8e93c05. Configure here.

@waleedlatif1 waleedlatif1 merged commit 05c1c5b into staging Apr 15, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/debug-agent-response branch April 15, 2026 19:21
Sg312 added a commit that referenced this pull request Apr 15, 2026
…mat, logs performance improvements

fix(csp): add missing analytics domains, remove unsafe-eval, fix workspace CSP gap (#4179)
fix(landing): return 404 for invalid dynamic route slugs (#4182)
improvement(seo): optimize sitemaps, robots.txt, and core web vitals across sim and docs (#4170)
fix(gemini): support structured output with tools on Gemini 3 models (#4184)
feat(brightdata): add Bright Data integration with 8 tools (#4183)
fix(mothership): fix superagent credentials (#4185)
fix(logs): close sidebar when selected log disappears from filtered list; cleanup (#4186)
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