Skip to content

Python: Honor the requested model in non-streamed Gemini responses - #9736

Open
Ayoub (Ayoubhm07) wants to merge 1 commit into
microsoft:mainfrom
Ayoubhm07:fix/gemini-nonstream-requested-model
Open

Ayoub (Ayoubhm07) wants to merge 1 commit into
microsoft:mainfrom
Ayoubhm07:fix/gemini-nonstream-requested-model

Conversation

@Ayoubhm07

Copy link
Copy Markdown
Contributor

Motivation & Context

Follow-up I offered in #9139. _get_response resolves the request model (:515) but never passes it to _process_generate_response (:527), which falls back to self.model, the client default (:1088). The streaming path has fallen back to the resolved model since #9139, so a gemini-2.5-flash client called with options={"model": "gemini-2.5-pro"} reports gemini-2.5-pro when streamed and gemini-2.5-flash when not, whenever the response carries no model_version.

Description & Review Guide

  • What are the major changes? _process_generate_response takes the request model as a keyword-only model and falls back to it before self.model. _get_response passes it.
  • What is the impact of these changes? Only a non-streamed call that overrides the model and gets no model_version back changes: it now reports the requested model. A call without the new keyword behaves as before. One test, parametrized over stream, pins that both paths report the requested model (stream=False fails on main); another pins the client-default fallback. Gemini tests: 264 passed, 10 skipped. Ruff, pyright, mypy, pyrefly, ty and zuban report nothing new at the pinned versions.
  • What do you want reviewers to focus on? The keyword-only argument with a None default, the same shape as the Bedrock fix in Python: fix(bedrock): report the requested model on non-streaming responses #9399, so existing calls are unaffected. This PR only touches Gemini; Bedrock stays with Python: fix(bedrock): report the requested model on non-streaming responses #9399.

Related Issue

Fixes #9548

AI Assistance

  • No material AI assistance was used.
  • This is an AI-assisted contribution. I reviewed, understood, and verified all submitted content and accept responsibility for it.

AI assistance details: used to draft the tests and this description. I checked that the new test fails on main, reviewed the change and ran the Gemini tests myself.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR links to an agreed issue with no competing open PR, or the Related Issue section documents a trivial-change or repository-automation exception.
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

_get_response resolved the request model but did not pass it to
_process_generate_response, which fell back to the client default when
the response carried no model_version. The streaming path already falls
back to the resolved model, so the same call was labelled differently
depending on stream.

_process_generate_response now takes the request model as a keyword-only
argument and falls back to it before the client default.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The narrow fix preserves existing callers and server-reported model precedence, with focused regression coverage for the corrected behavior.

No open issues 🎉

What changed in this PR

Fixes #9548 by making Gemini non-streaming responses report the requested model when the API omits model_version, matching streaming behavior.

Changes:

  • Passes the resolved request model into non-streaming response processing.
  • Preserves server-reported model precedence and the existing client-default fallback.
  • Adds regression tests for per-call model overrides on both paths and the client-default fallback.
File Description
python/​packages/​gemini/​tests/​test_gemini_client.py Adds model-fallback and streaming/non-streaming parity tests.
python/​packages/​gemini/​agent_framework_gemini/​_chat_client.py Uses the requested model before the client default when processing responses.

Review effort: Balanced

This branch was successfully deployed

1 active deployment
github-app-auth — f517a8f5 Deployed Oct 26, 2026 by Ayoubhm07 via team_check #7586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Gemini non-streaming responses report the client default model instead of the requested one

2 participants