Skip to content

fix(ai-gemini): end MALFORMED_FUNCTION_CALL and LANGUAGE finishes with RUN_ERROR - #2234

Merged
AlemTuzlak merged 3 commits into
TanStack:mainfrom
pcbeingused33:fix/gemini-abnormal-finish-reason
Nov 30, 2026
Merged

AlemTuzlak merged 3 commits into
TanStack:mainfrom
pcbeingused33:fix/gemini-abnormal-finish-reason

Conversation

@pcbeingused33

@pcbeingused33 pcbeingused33 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

Follows #2183, which did the same for Mistral.

The Gemini text adapter ended every finish reason other than MAX_TOKENS and the content-filter set as RUN_FINISHED with stop. When Gemini finishes a turn with MALFORMED_FUNCTION_CALL (it could not form the function call) or LANGUAGE (unsupported language), the chunk has no text and no tool call. The run then looks like a normal stop, and an agent loop ends with no output and no error.

These two finish reasons now end the run with a RUN_ERROR (codes malformed_function_call and language). The error carries usage and no RUN_FINISHED follows, the same way MAX_TOKENS already works and like the Mistral adapter handles its error finish reason. OTHER stays a normal stop, as the existing test requires. docs/adapters/gemini.md lists the two new codes next to max_tokens.

Test plan

The @tanstack/ai-gemini vitest suite (68 tests) passes with the change; the full pnpm run test:pr was not run. It includes the new parameterized test in tests/gemini-adapter.test.ts. It streams a chunk with each finish reason and checks for a RUN_ERROR with the right code and usage, and no RUN_FINISHED. Both cases fail without the change in src/adapters/text.ts.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

This pull request was prepared with the assistance of an AI agent (Claude).

Summary by CodeRabbit

  • Bug Fixes
    • Gemini responses ending with MALFORMED_FUNCTION_CALL or LANGUAGE now report an error instead of indicating successful completion. The error includes usage information and a reason-specific code.

…h RUN_ERROR

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4e5da6e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@tanstack/ai-gemini Patch
@tanstack/ai-optimize-evals Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Gemini adapter now emits RUN_ERROR events with corresponding error codes and usage for MALFORMED_FUNCTION_CALL and LANGUAGE finish reasons. Unit tests, end-to-end tests, and documentation cover these outcomes.

Changes: Gemini finish reason handling

Layer / File(s) Summary
Map and report abnormal finish reasons
packages/ai-gemini/src/adapters/text.ts, packages/ai-gemini/tests/gemini-adapter.test.ts, docs/adapters/gemini.md, .changeset/gemini-abnormal-finish-reason.md
The adapter maps MALFORMED_FUNCTION_CALL and LANGUAGE to error details and emits RUN_ERROR with usage. Unit tests assert that these reasons do not produce RUN_FINISHED. Documentation and a patch changeset record the outcomes.
Exercise abnormal finish scenarios
testing/e2e/src/routes/api.gemini-abnormal-finish-reason.$.ts, testing/e2e/src/routeTree.gen.ts, testing/e2e/tests/gemini-abnormal-finish-reason.spec.ts
The new route simulates abnormal finish responses and collects chat outcomes. The route tree registers the endpoint. End-to-end tests check the error code, usage, and event counts.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: tombeckenham, alemtuzlak

Merge Risk: Minimal · up to 4e5da

Gemini replies that end with a malformed function call or a language finish reason now surface as errors instead of ending silently. The change is small, tested and documented, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Gemini text adapter now ends MALFORMED_FUNCTION_CALL and LANGUAGE finishes with RUN_ERROR instead of RUN_FINISHED. The raw summary confirms this change in packages/ai-…
Description check Passed The description follows the template. It explains the change and why it is needed, and it lists the new error codes. It includes a test plan and marks the checklist items. It also documents that the d…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Gemini text adapter now ends MALFORMED_FUNCTION_CALL and LANGUAGE finishes with RUN_ERROR instead of RUN_FINISHED. The raw summary confirms this change in packages/ai-gemini/src/adapters/text.ts, with matching tests and docs. The title is specific and follows the fix(scope) convention.

Full details: Description check

Explanation

The description follows the template. It explains the change and why it is needed, and it lists the new error codes. It includes a test plan and marks the checklist items. It also documents that the docs and changeset were updated, which matches the raw summary. One checklist item stays unchecked: the author did not run pnpm run test:pr, and the description says so. The description states this limitation clearly, so the template is still satisfied.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: TanStack/ai
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 7fbfa5d
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 401dd292-c8da-4f69-97a1-3a22ede5490b
Commits

Reviewing files that changed from the base of the PR and between 2c7c051 and 4e5da6e.

Files selected for processing: 1

  • testing/e2e/src/routeTree.gen.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

@github-actions

Copy link
Copy Markdown
Contributor

Thanks for the PR, @pcbeingused333! 🙌 @jherr will take a look.

Automated pre-review checks

  • ✅ CI passing
  • ✅ No merge conflicts
  • ✅ Changeset present
  • ⚠️ No E2E test changes detected — behavior changes need coverage under testing/e2e/ (see CONTRIBUTING)

Automated triage — a human review follows.

@github-actions github-actions Bot added the waiting-on: maintainer The ball is in the maintainers’ court label Nov 23, 2026
@pcbeingused33

Copy link
Copy Markdown
Contributor Author

Added E2E coverage for both finish reasons, as the triage check asked: testing/e2e/tests/gemini-abnormal-finish-reason.spec.ts runs chat() with a server tool against a mocked Gemini stream and checks the RUN_ERROR code and usage, that onError fires once, that there is no RUN_FINISHED or onFinish, and that the tool never runs. The MAX_TOKENS policy is unchanged and is not widened in this PR.

This comment was prepared with assistance from an AI coding tool.

@nx-cloud

nx-cloud Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 4e5da6e

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 3m 8s View ↗
nx run-many --targets=build --exclude=examples/... ✅ Succeeded 15s View ↗
nx run-many --targets=test:coverage --projects=... ✅ Succeeded 3s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-30 09:19:40 UTC

@AlemTuzlak
AlemTuzlak merged commit c374f07 into TanStack:main Nov 30, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-on: maintainer The ball is in the maintainers’ court

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants