Skip to content

fix: make Gemini max-tokens optional and clarify max-token errors#81

Merged
giswqs merged 6 commits into
mainfrom
fix/gemini-max-tokens-handling
May 8, 2026
Merged

fix: make Gemini max-tokens optional and clarify max-token errors#81
giswqs merged 6 commits into
mainfrom
fix/gemini-max-tokens-handling

Conversation

@giswqs

@giswqs giswqs commented May 7, 2026

Copy link
Copy Markdown
Member

Summary

  • Treat max_tokens as optional across providers. For Gemini in particular, omit max_output_tokens when no explicit limit is set so the provider's native output budget is used. Explicit user values are preserved as-is.
  • Add an Auto sentinel (0) to the QGIS settings spinbox so users can opt into provider-default behavior. Positive values below the historical 256-token minimum are clamped up to 256 on save so the saved config never holds a tiny limit the spinbox previously disallowed.
  • Convert opaque max-token stops into actionable guidance in both the core agent and the QGIS chat dock. Streaming runs route the same guidance through _format_chat_worker_error so users see it instead of a truncated answer. Detection markers are now shared between the core helper and the UI.

Test plan

  • pre-commit run --all-files
  • pytest tests/test_model_providers.py qgis_geoagent/tests/test_chat_tool_inputs.py -q
  • Manual: trigger a long Gemini tool workflow that previously stopped on max_tokens and confirm the new guidance is shown.

Gemini agentic tool runs were failing with opaque "max_tokens limit"
errors when the configured max output tokens were too small for
multi-step tool calls. Lift Gemini's effective output budget to a
practical floor and surface clear guidance in both the core agent
and the QGIS chat dock when a model output cap is hit.
Copilot AI review requested due to automatic review settings May 7, 2026 21:23
@github-actions

github-actions Bot commented May 7, 2026

Copy link
Copy Markdown

@github-actions github-actions Bot temporarily deployed to pull request May 7, 2026 21:25 Inactive

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.

Pull request overview

This PR improves the Gemini provider experience by (1) ensuring Gemini has a practical minimum output-token budget for agentic/tool workflows, and (2) translating “max tokens reached” failures into actionable guidance in both the core library and the QGIS chat UI, with accompanying unit tests.

Changes:

  • Enforce an 8192 minimum max_output_tokens for Gemini while preserving larger user-configured limits.
  • Add max-token stop detection and clearer remediation guidance in geoagent and the QGIS chat dock.
  • Add unit tests covering the Gemini token floor and the QGIS error-formatting branch.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
geoagent/core/model.py Adds Gemini output-token floor helper and applies it during model resolution.
geoagent/core/agent.py Detects max-token stop errors and formats them into user-facing guidance.
qgis_geoagent/open_geoagent/dialogs/chat_dock.py Adds UI-side formatting for max-token stop-related errors (incl. Gemini-specific advice).
tests/test_model_providers.py Tests Gemini token-floor behavior and preservation of larger explicit limits.
qgis_geoagent/tests/test_chat_tool_inputs.py Tests QGIS chat-dock error formatter for Gemini max-token-stop messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread qgis_geoagent/open_geoagent/dialogs/chat_dock.py Outdated
Comment thread qgis_geoagent/open_geoagent/dialogs/chat_dock.py Outdated
- Streaming path now treats `stop_reason=max_tokens` as a failure and routes
  the error through `_format_chat_worker_error`, so users see the same
  actionable max-token guidance during streamed runs instead of a truncated
  answer.
- Replace the duplicated max-token marker list in `_format_chat_worker_error`
  with the shared `_looks_like_max_tokens_reached` helper from
  `geoagent.core.agent`, keeping the QGIS UI and core agent in sync (now
  also catches `stop reason: max_tokens`).
- Add a test covering both `stop_reason=max_tokens` and `stop reason: max_tokens`
  phrasings so the two paths cannot drift again.
@github-actions github-actions Bot temporarily deployed to pull request May 7, 2026 22:29 Inactive
@giswqs giswqs requested a review from Copilot May 8, 2026 00:40
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 00:41 Inactive

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.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Comment thread geoagent/core/model.py
Comment thread qgis_geoagent/open_geoagent/dialogs/settings_dock.py
- Clamp positive max-token values below 256 up to 256 in
  `_max_tokens_to_setting`. The settings spinbox now uses 0 as the Auto
  sentinel, which lets users type 1-255 that the previous min=256 spinbox
  rejected. Saving normalizes those into the historical 256-token floor so
  the saved config never holds a value the spinbox previously disallowed.
- Add a unit test that pins the clamp behavior and the Auto sentinel.
@giswqs giswqs changed the title fix: explain Gemini max-tokens stops and raise output budget floor fix: make Gemini max-tokens optional and clarify max-token errors May 8, 2026
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 01:13 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 8, 2026 01:51 Inactive
@giswqs giswqs merged commit 21fc434 into main May 8, 2026
9 checks passed
@giswqs giswqs deleted the fix/gemini-max-tokens-handling branch May 8, 2026 01:52
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