Skip to content

fix(google): read serviceTier from usageMetadata in stream + generate#15488

Merged
shaper merged 2 commits into
mainfrom
google-stream-service-tier-usage-metadata
May 20, 2026
Merged

fix(google): read serviceTier from usageMetadata in stream + generate#15488
shaper merged 2 commits into
mainfrom
google-stream-service-tier-usage-metadata

Conversation

@shaper
Copy link
Copy Markdown
Collaborator

@shaper shaper commented May 20, 2026

Summary

  • The Gemini API returns the applied service tier in two places: the x-gemini-service-tier response header (non-streaming only) and usageMetadata.serviceTier in the response body (both streaming and non-streaming).
  • The previous implementation only read the header, so providerMetadata.google.serviceTier was always null on streamText.
  • Read usageMetadata.serviceTier from the body for both doGenerate and doStream. Single source of truth, no header involvement, matches the pattern Vertex already uses (usageMetadata.trafficType).
  • Docs updated to drop the implementation detail about where the value comes from.

Test plan

  • pnpm test in packages/google — 583 tests pass (node + edge), incl. two new tests asserting serviceTier is populated from usageMetadata in both generate and stream paths.
  • pnpm type-check:full — clean.
  • Verified live against the Gemini API:
    • pnpm tsx src/generate-text/google/service-tier.tsserviceTier: priority (still works after dropping header read)
    • pnpm tsx src/stream-text/google/service-tier.tsserviceTier: priority (previously null)
  • Verified Vertex examples unaffected:
    • pnpm tsx src/generate-text/google/vertex-service-tier.tstrafficType: ON_DEMAND_PRIORITY
    • pnpm tsx src/stream-text/google/vertex-service-tier.tstrafficType: ON_DEMAND_PRIORITY

🤖 Generated with Claude Code

…generate and stream paths

The previous implementation read `serviceTier` from the `x-gemini-service-tier`
response header, which is only populated on non-streaming responses. Gemini
streaming includes the value in `usageMetadata.serviceTier` on every chunk, so
`providerMetadata.google.serviceTier` was always `null` for streams. Read from
`usageMetadata` for both paths instead.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@shaper shaper merged commit 045d2e8 into main May 20, 2026
48 checks passed
@shaper shaper deleted the google-stream-service-tier-usage-metadata branch May 20, 2026 23:33
@shaper shaper added the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 20, 2026
github-actions Bot added a commit that referenced this pull request May 20, 2026
@github-actions github-actions Bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label May 20, 2026
@github-actions
Copy link
Copy Markdown
Contributor

⚠️ Backport to release-v6.0 created but has conflicts: #15490

shaper added a commit that referenced this pull request May 20, 2026
- Keep release-v6.0 file structure (providerOptionsName indirection,
  GoogleGenerativeAI* type names, GoogleGenerativeAITokenDetail).
- Adopt the actual semantic change from #15488: read `serviceTier`
  from `usageMetadata.serviceTier` (body) in both doGenerate and
  doStream, instead of the `x-gemini-service-tier` response header.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
shaper added a commit that referenced this pull request May 20, 2026
…+ generate (#15490)

This is an automated backport of #15488 to the release-v6.0 branch. FYI
@shaper
This backport has conflicts that need to be resolved manually.

### `git cherry-pick` output

```
Auto-merging content/providers/01-ai-sdk-providers/15-google.mdx
Auto-merging packages/google/src/convert-google-generative-ai-usage.ts
CONFLICT (content): Merge conflict in packages/google/src/convert-google-generative-ai-usage.ts
Auto-merging packages/google/src/google-generative-ai-language-model.test.ts
Auto-merging packages/google/src/google-generative-ai-language-model.ts
CONFLICT (content): Merge conflict in packages/google/src/google-generative-ai-language-model.ts
error: could not apply 045d2e8... fix(google): read serviceTier from usageMetadata in stream + generate (#15488)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
```

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Walter Korman <shaper@vercel.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown
Contributor

🚀 Published in:

Package Version
@ai-sdk/google 4.0.0-canary.71 github npm
@ai-sdk/google-vertex 5.0.0-canary.90 github npm

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