Repository navigation
feat: add Sarvam AI provider (chat completions) #5064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Shaik-Sirajuddin
wants to merge
7
commits into
maximhq:dev
from
Shaik-Sirajuddin:feature/sarvam-provider
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
efc7554
feat: add Sarvam AI provider (chat + TTS + STT, sync + WS streaming)
Shaik-Sirajuddin 14b552d
refactor: scope this branch to Sarvam /v1/chat only
Shaik-Sirajuddin d5c9cd9
fix: send Sarvam's native auth header on ListModels
Shaik-Sirajuddin f61111a
feat(ui): register Sarvam AI as a known provider in the dashboard
Shaik-Sirajuddin 29cdd81
fix: raise stream chunk safety cap for Sarvam instead of skipping tests
Shaik-Sirajuddin 3376758
fix: address bot review findings on Sarvam PR
Shaik-Sirajuddin 3bd2ece
fix: normalize request shape for two Sarvam wire-compatibility gaps
Shaik-Sirajuddin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| # Sarvam provider — deferred work | ||
|
|
||
| ## Reasoning-token usage breakdown not reported | ||
|
|
||
| **Status:** not implemented, intentionally deferred pending a concrete need. | ||
|
|
||
| **What's missing:** `schemas.ChatCompletionTokensDetails.ReasoningTokens` (and `.ReasoningTokensCost`) | ||
| are always `0` for Sarvam responses. This is the same field the Anthropic | ||
| thinking-tokens usage fix (this repo, PR preceding this branch) populates | ||
| from `output_tokens_details.thinking_tokens`. | ||
|
|
||
| **Why:** Sarvam's `usage.completion_tokens_details` is always `null` in every | ||
| response observed (verified against both live responses and Sarvam's | ||
| published OpenAPI spec — `CompletionUsage.completion_tokens_details` is a | ||
| loosely-typed open object with no fixed schema, but Sarvam never populates | ||
| it). There is no other field carrying a reasoning-token count anywhere in | ||
| Sarvam's chat completion response — `ChatCompletionResponseMessage` pairs | ||
| `reasoning_content` (the raw text) with no accompanying count. | ||
|
|
||
| **What is correct today:** `usage.completion_tokens` / `usage.total_tokens` | ||
| already include reasoning-token consumption in the total (confirmed live: a | ||
| request that spent ~999 of 1000 completion tokens on reasoning reported | ||
| `completion_tokens: 1001`, matching the real total). Budget enforcement, | ||
| quota tracking, and cost totals that operate on the aggregate token count are | ||
| unaffected. What's missing is purely the *category breakdown* (reasoning vs. | ||
| output) for reporting/attribution — not the total itself. | ||
|
|
||
| **Considered and rejected (for now):** client-side estimation, i.e. | ||
| tokenizing `reasoning_content` ourselves to synthesize a `ReasoningTokens` | ||
| value Sarvam never sent. Rejected because: | ||
| - It would be an estimate against an unknown tokenizer (Sarvam doesn't | ||
| publish one), so the number could be meaningfully wrong — arguably worse | ||
| for a billing/governance field than reporting zero. | ||
| - No other provider without native `reasoning_tokens` gets this estimation | ||
| treatment in Bifrost today; adding it only for Sarvam would be a one-off | ||
| inconsistency, not a documented pattern. | ||
| - Out of the original scope (chat/TTS/STT wire compatibility) — this | ||
| surfaced from a governance question during review, not a filed request. | ||
| - Adds a new tokenizer dependency for a single provider's cosmetic field. | ||
|
|
||
| **When to revisit:** if there's a concrete downstream need for | ||
| per-category (thinking vs. output) cost attribution/reporting specifically | ||
| for Sarvam usage, and an approximate number is acceptable. If so: | ||
| 1. Pick a tokenizer approximation (e.g. reuse whatever the repo already | ||
| vendors for other estimation needs, if any). | ||
| 2. Populate `ChatCompletionTokensDetails.ReasoningTokens` from a token count | ||
| of `message.reasoning_content` in `core/providers/sarvam/sarvam.go`'s | ||
| chat path (would need a thin wrapper around | ||
| `openai.HandleOpenAIChatCompletionRequest`'s response, since that call is | ||
| currently a straight passthrough with no Sarvam-specific post-processing). | ||
| 3. Clearly label the value as estimated (not authoritative) wherever it | ||
| surfaces, so it isn't confused with a real provider-reported count. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package sarvam | ||
|
|
||
| import ( | ||
| providerUtils "github.com/maximhq/bifrost/core/providers/utils" | ||
| "github.com/maximhq/bifrost/core/schemas" | ||
| ) | ||
|
|
||
| // CachedContentCreate is unsupported on SarvamProvider. Only Gemini and Vertex AI | ||
| // implement the cached-content lifecycle (Google AI Studio + Vertex AI named | ||
| // caches). Sarvam has no equivalent. | ||
| func (provider *SarvamProvider) CachedContentCreate(ctx *schemas.BifrostContext, key schemas.Key, request *schemas.BifrostCachedContentCreateRequest) (*schemas.BifrostCachedContentCreateResponse, *schemas.BifrostError) { | ||
| return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentCreateRequest, provider.GetProviderKey()) | ||
| } | ||
|
|
||
| // CachedContentList is unsupported on SarvamProvider (see CachedContentCreate). | ||
| func (provider *SarvamProvider) CachedContentList(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentListRequest) (*schemas.BifrostCachedContentListResponse, *schemas.BifrostError) { | ||
| return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentListRequest, provider.GetProviderKey()) | ||
| } | ||
|
|
||
| // CachedContentRetrieve is unsupported on SarvamProvider (see CachedContentCreate). | ||
| func (provider *SarvamProvider) CachedContentRetrieve(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentRetrieveRequest) (*schemas.BifrostCachedContentRetrieveResponse, *schemas.BifrostError) { | ||
| return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentRetrieveRequest, provider.GetProviderKey()) | ||
| } | ||
|
|
||
| // CachedContentUpdate is unsupported on SarvamProvider (see CachedContentCreate). | ||
| func (provider *SarvamProvider) CachedContentUpdate(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentUpdateRequest) (*schemas.BifrostCachedContentUpdateResponse, *schemas.BifrostError) { | ||
| return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentUpdateRequest, provider.GetProviderKey()) | ||
| } | ||
|
|
||
| // CachedContentDelete is unsupported on SarvamProvider (see CachedContentCreate). | ||
| func (provider *SarvamProvider) CachedContentDelete(ctx *schemas.BifrostContext, keys []schemas.Key, request *schemas.BifrostCachedContentDeleteRequest) (*schemas.BifrostCachedContentDeleteResponse, *schemas.BifrostError) { | ||
| return nil, providerUtils.NewUnsupportedOperationError(schemas.CachedContentDeleteRequest, provider.GetProviderKey()) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package sarvam | ||
|
|
||
| import ( | ||
| providerUtils "github.com/maximhq/bifrost/core/providers/utils" | ||
| schemas "github.com/maximhq/bifrost/core/schemas" | ||
| "github.com/valyala/fasthttp" | ||
| ) | ||
|
|
||
| // parseSarvamError parses Sarvam's error envelope: {"error":{"message","code","request_id"}}. | ||
| func parseSarvamError(resp *fasthttp.Response) *schemas.BifrostError { | ||
| var errorResp SarvamError | ||
| bifrostErr := providerUtils.HandleProviderAPIError(resp, &errorResp) | ||
| if errorResp.Error != nil { | ||
| if bifrostErr.Error == nil { | ||
| bifrostErr.Error = &schemas.ErrorField{} | ||
| } | ||
| bifrostErr.Error.Message = errorResp.Error.Message | ||
| bifrostErr.Error.Type = new(errorResp.Error.Code) | ||
| } | ||
| return bifrostErr | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.