fix: exclude undefined cost from providerMetadata#359
Merged
robert-j-y merged 7 commits intomainfrom Jan 27, 2026
Merged
Conversation
When cost is undefined, it should not be included in the providerMetadata object since undefined is not a valid JSON value. This was causing AI SDK validation failures when passing providerMetadata back into subsequent conversation turns. The fix uses conditional spreading/assignment to only include the cost field when it has a defined value, matching the existing pattern used for other optional fields like costDetails. Fixes #262 Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
mattapperson
approved these changes
Jan 27, 2026
Ensure embedding model also uses conditional spreading for cost field to maintain consistency with chat and completion models. Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
For consistency with chat model and streaming endpoints, the completion model's doGenerate method now returns providerMetadata with usage info. Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
… models - Completion doGenerate: use OpenRouterProviderMetadataSchema.parse() with provider field - Completion doStream: track and include provider field in providerMetadata - Completion schema: add provider field - Embedding model: include provider field and full usage structure (promptTokens, completionTokens, totalTokens) - Embedding schema: add provider field - Update embedding test expectations Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai>
Merged
kesavan-byte
pushed a commit
to osm-API/ai-sdk-provider
that referenced
this pull request
Feb 13, 2026
* fix: exclude undefined cost from providerMetadata When cost is undefined, it should not be included in the providerMetadata object since undefined is not a valid JSON value. This was causing AI SDK validation failures when passing providerMetadata back into subsequent conversation turns. The fix uses conditional spreading/assignment to only include the cost field when it has a defined value, matching the existing pattern used for other optional fields like costDetails. Fixes OpenRouterTeam#262 Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> * fix: apply same cost fix to embedding model Ensure embedding model also uses conditional spreading for cost field to maintain consistency with chat and completion models. Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> * fix: add providerMetadata to completion model doGenerate For consistency with chat model and streaming endpoints, the completion model's doGenerate method now returns providerMetadata with usage info. Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> * fix: add schema validation and provider field to completion/embedding models - Completion doGenerate: use OpenRouterProviderMetadataSchema.parse() with provider field - Completion doStream: track and include provider field in providerMetadata - Completion schema: add provider field - Embedding model: include provider field and full usage structure (promptTokens, completionTokens, totalTokens) - Embedding schema: add provider field - Update embedding test expectations Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> * fix: add schema validation to embedding model for consistency Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> * test: add coverage for completion doGenerate providerMetadata Co-Authored-By: Robert Yeakel <robert.yeakel@openrouter.ai> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #262 - When models like
gemini-3-pro-previewdon't return acostvalue, theproviderMetadataobject was includingcost: undefined. Sinceundefinedis not a valid JSON value, this caused AI SDK validation failures when passingproviderMetadataback into subsequent conversation turns.Before:
After:
The fix uses conditional spreading/assignment to only include
costwhen it has a defined value, matching the existing pattern used for other optional fields likecostDetails.Changes Summary
costin bothdoGenerateanddoStreamproviderMetadatawith schema validation indoGenerate(was missing entirely)providerfield tracking indoStreamproviderfield to completion schemaproviderMetadatawith full usage structure and schema validation viaOpenRouterProviderMetadataSchema.parse()providerfield to schemaUpdates since last revision
doGenerateproviderMetadata:should return providerMetadata with usage and provider- verifies basic structureshould omit cost from providerMetadata when undefined- verifies the main bug fixshould include cost: 0 in providerMetadata when cost is zero- verifies zero cost is preservedshould default provider to empty string when not returned by API- verifies fallback behaviorshould include token details in providerMetadata when provided- verifies full feature coverageHuman Review Checklist
cost: 0is still correctly included (the!= nullcheck allows this)src/chat/index.ts- doGenerate (line ~485) and doStream (line ~703)src/completion/index.ts- doGenerate (line ~224, uses schema validation) and doStream (line ~419, tracks provider)src/embedding/index.ts- doEmbed (line ~93, uses schema validation)providerMetadatawith full structure (previouslyundefinedwhen no cost)doGeneratenow returnsproviderMetadatawith schema validation (previously didn't return it at all)providerfield (defaults to''when not returned by API)doStreammethods build metadata manually without schema validation (matches existing chat model pattern for streaming performance)Link to Devin run: https://app.devin.ai/sessions/7543a73deec84146abe08abf555d19a9
Requested by: Robert Yeakel (@robert-j-y)
Checklist
pnpm stylecheckandpnpm typecheckpnpm testand all tests passChangeset
pnpm changesetto create a changeset file