Skip to content

fix(provider): escape the slash in a layered agentgateway Gemini model name - #491

Open
dimetron wants to merge 2 commits into
mainfrom
fix/agentgateway-gemini-layered-model
Open

dimetron wants to merge 2 commits into
mainfrom
fix/agentgateway-gemini-layered-model

Conversation

@dimetron

Copy link
Copy Markdown
Owner

The bug

pi --model agentgateway/gemini/gemini-4-neon "hi" failed with an empty 404, while the unlayered agentgateway/gemini-4-neon worked:

$ pi --mode print --model agentgateway/gemini/gemini-4-neon "say hi"
Error: agent run: Error 404, Message: , Status: 404 Not Found, Details: []

$ pi --mode print --model agentgateway/gemini-4-neon "say hi"
Hi! What are we working on today?

This also affects the shape the gateway's own pi-fast failover uses (gemini/gemini-3.8-flash).

Root cause

Resolve strips only the outer agentgateway/ wrapper, so a layered route reaches the native Gemini client as gemini/<id>. The genai SDK pastes the model into the URL path and escapes nothing (createAPIURL → url.JoinPath), so the slash splits the request into an extra segment:

/v1beta/models/gemini/gemini-4-neon:generateContent   -> 404, empty body
/v1beta/models/gemini%20...%2Fgemini-4-neon:...        -> 200

The gateway resolves a layered name on its OpenAI-compatible endpoint, where the model is a JSON field, but not on the native one. The native path is required — it exists so Gemini's server-side google_search tool is not silently dropped — so switching endpoints is not a fix.

A genuine unknown model returns a JSON NOT_FOUND naming the model; this 404 had content-length: 0. Nothing in it named the model or the slash, which is what makes it worth fixing rather than documenting.

The fix

url.PathEscape the model name per request, on the wire only. Two deliberate choices:

  • Not on Name(). That string is user-facing and reaches session metadata (SetSessionModel) and the TUI model label; a %2F there would be a second, cosmetic bug.
  • Per request, not in NewGemini. ADK rebuilds every request as LLMRequest{Model: f.Model.Name()} (base_flow.go:430,793), so escaping at construction would be overwritten on the next turn. The request value is copied, not mutated, because the same pointer feeds tracing hooks.

url.PathEscape rather than a slash replacement: it escapes exactly the characters that would change the URL's structure (/ ? #) and leaves colons, dashes and dots in an id alone. An unlayered id has no slash and is byte-for-byte unchanged, so this is a no-op for the common case.

Verification

The regression test asserts on r.URL.EscapedPath(), not r.URL.Path — the distinction is the bug, since Go's URL decodes %2F back to / in Path, so a test reading Path cannot tell the two requests apart. It fails on the unfixed code with the exact bad path, and passes with the fix.

End to end against the local gateway, the gateway's own log:

OLD binary: http.path=/v1beta/models/gemini/gemini-4-neon:streamGenerateContent   404
NEW binary: http.path=/v1beta/models/gemini%2Fgemini-4-neon:streamGenerateContent  200
            gen_ai.request.model=gemini-4-neon  agw.ai.original_model=gemini/gemini-4-neon
  • go build ./... — clean
  • go test ./internal/provider/ ./internal/agent/ — pass
  • go test over all 69 remaining packages — pass, 0 failures
  • make vet — clean
  • make lint — 0 issues

internal/cli hangs in this environment, on unmodified main as well (300s timeout, blocked reading /dev/tty); it is excluded from the run above and is unrelated to this change.

Note on the gateway

This fixes the pi-go side. The gateway still does not strip a provider prefix on the native generateContent path the way it does for /v1/chat/completions, so any other client sending a layered name unescaped will hit the same empty 404.

…l name

`pi --model agentgateway/gemini/gemini-4-neon` failed with an empty 404 while
the unlayered `agentgateway/gemini-4-neon` worked.

Resolve strips only the outer agentgateway/ wrapper, so a layered route reaches
the native Gemini client as `gemini/<id>`. The genai SDK pastes the model into
the URL path and escapes nothing (createAPIURL -> url.JoinPath), so the slash
split the request into an extra segment:

    /v1beta/models/gemini/gemini-4-neon:generateContent   -> 404, empty body

The gateway resolves a layered name on its OpenAI-compatible endpoint, where
the model is a JSON field, but not on the native one. Escaping the slash keeps
it one path segment and the gateway routes and strips it correctly:

    /v1beta/models/gemini%2Fgemini-4-neon:...             -> 200

Nothing in the 404 named the model or the slash, which is what makes this worth
fixing rather than documenting. Verified against the local gateway: the fixed
binary answers, the installed one still 404s.

The escape is applied per request on the wire, not to Name(). That string is
user-facing and reaches session metadata and the TUI label, so a %2F there
would be a second bug; ADK also rebuilds every request as
LLMRequest{Model: f.Model.Name()}, so escaping inside NewGemini alone would be
overwritten on the next turn. An unlayered id has no slash and is unchanged.

url.PathEscape rather than a slash replacement: it escapes exactly the
characters that would change the URL's structure and leaves colons, dashes and
dots in an id alone.

Signed-off-by: pi <dimetron@me.com>

@dimetron dimetron left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reviewed git diff main...HEAD at ac85e83. One actionable P2 finding is attached inline.

Standards

The wrapper breaks ADK Gemini's supported request-model precedence, exposed through before-model callbacks. No additional actionable standards findings.

Spec

The same finding affects the promised per-request escaping: the constructor name is escaped instead of the effective request name. No additional spec findings.

Validation: go test ./internal/provider/ ./internal/agent/ -timeout 180s passed (1,785 tests). A temporary Go test overlay reproduced both override failures against an HTTP test server; the native Gemini control passed. Tracked files remain unchanged.

— Codex review

Comment thread internal/provider/agentgateway.go Resolved
@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.21%. Comparing base (6d236a5) to head (416532b).

Files with missing lines Patch % Lines
internal/provider/agentgateway.go 92.85% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #491      +/-   ##
==========================================
- Coverage   92.21%   92.21%   -0.01%     
==========================================
  Files         426      426              
  Lines       57744    57757      +13     
==========================================
+ Hits        53251    53261      +10     
- Misses       4487     4490       +3     
  Partials        6        6              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…onfigured name

The wire-name wrapper wrote its configured model name into every request,
which discarded a req.Model set by a BeforeModelCallback. The genai SDK honors
that override, so an overridden call silently went to a different model than
the caller asked for — a worse failure than the 404 this wrapper fixes, since
the answer comes back from a model nobody requested.

Escape the requested name instead, falling back to the configured one only when
the request names none, which is the case the SDK itself falls back on. A
layered request retargeted to a bare id now drops the escape, and a bare route
retargeted to a layered id gains one.

Found by Codex review on PR #491; reproduced against the pre-fix wrapper before
fixing.

Signed-off-by: pi <dimetron@me.com>

@dimetron dimetron left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Found one issue, and it is already fixed on the branch (416532b). Recording it here with the inline anchor so the finding and its resolution stay in one place.

req.Model override is discarded. gatewayGeminiWireName.GenerateContent wrote the configured model name into every request, ignoring a req.Model set by a BeforeModelCallback. The genai SDK honors that override (geminiModel.modelName prefers req.Model), so an overridden call would silently go to a different model than the caller asked for — worse than the 404 this wrapper fixes, because the answer comes back from a model nobody requested.

Reproduced against the pre-fix wrapper:

gateway=gemini/gemini-4-neon      req.Model=gemini-3.8-flash      -> /v1beta/models/gemini%2Fgemini-4-neon:generateContent
gateway=gemini-4-neon            req.Model=gemini/gemini-3.8-flash -> /v1beta/models/gemini/gemini-3.8-flash:generateContent

Both paths are wrong: the first sends the original model despite the override, the second drops the escape the override needs.

Fixed by escaping the requested name, falling back to the configured one only when the request names none — the same fallback the SDK makes. TestAgentGatewayGeminiWireNameHonorsRequestOverride covers all three cases and fails on the pre-fix wrapper.

The rest of the change verified clean: the escaping itself is correct and confirmed against the live gateway (/v1beta/models/gemini%2Fgemini-4-neon:streamGenerateContent → 200), the guard asserts on r.URL.EscapedPath() for the right reason, and Name() is correctly left unescaped so the TUI and session metadata keep the name the user typed.

— Codex review

Comment thread internal/provider/agentgateway.go Resolved
@dimetron

Copy link
Copy Markdown
Owner Author

govulncheck

Scanner: govulncheck@v1.10.0 on Go 1.27.4 · DB: https://vuln.go.dev, updated 2026-11-11 02:51 UTC

No findings with an available fix. The gate passes.

9 findings are reported but do not fail, because no fixed version exists to upgrade to — all of them in github.com/ollama/ollama:

GO-2025-3557  GO-2025-3558  GO-2025-3559  GO-2025-3582  GO-2025-3689
GO-2025-3695  GO-2025-3824  GO-2025-4251  GO-2026-5750

The answer is only true for this database snapshot; the set will move as upstream cuts releases.

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.

1 participant