Skip to content

Commit dc45a26

Browse files
committed
fix: remove stream_options from xAI provider to fix Grok 4 API errors
- Removed stream_options: { include_usage: true } from XAIHandler - Grok models do not support the stream_options parameter - This fixes API errors when using Grok 4 model - Updated tests to reflect the change Fixes #6211
1 parent d62a260 commit dc45a26

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

src/api/providers/__tests__/xai.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ describe("XAIHandler", () => {
276276
temperature: 0,
277277
messages: expect.arrayContaining([{ role: "system", content: systemPrompt }]),
278278
stream: true,
279-
stream_options: { include_usage: true },
280279
}),
281280
)
282281
})

src/api/providers/xai.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ export class XAIHandler extends BaseProvider implements SingleCompletionHandler
4848
const { id: modelId, info: modelInfo, reasoning } = this.getModel()
4949

5050
// Use the OpenAI-compatible API.
51+
// Note: Grok models don't support stream_options parameter
5152
const stream = await this.client.chat.completions.create({
5253
model: modelId,
5354
max_tokens: modelInfo.maxTokens,
5455
temperature: this.options.modelTemperature ?? XAI_DEFAULT_TEMPERATURE,
5556
messages: [{ role: "system", content: systemPrompt }, ...convertToOpenAiMessages(messages)],
5657
stream: true,
57-
stream_options: { include_usage: true },
5858
...(reasoning && reasoning),
5959
})
6060

@@ -78,12 +78,15 @@ export class XAIHandler extends BaseProvider implements SingleCompletionHandler
7878
if (chunk.usage) {
7979
// Extract detailed token information if available
8080
// First check for prompt_tokens_details structure (real API response)
81-
const promptDetails = "prompt_tokens_details" in chunk.usage ? chunk.usage.prompt_tokens_details : null;
82-
const cachedTokens = promptDetails && "cached_tokens" in promptDetails ? promptDetails.cached_tokens : 0;
81+
const promptDetails = "prompt_tokens_details" in chunk.usage ? chunk.usage.prompt_tokens_details : null
82+
const cachedTokens = promptDetails && "cached_tokens" in promptDetails ? promptDetails.cached_tokens : 0
8383

8484
// Fall back to direct fields in usage (used in test mocks)
85-
const readTokens = cachedTokens || ("cache_read_input_tokens" in chunk.usage ? (chunk.usage as any).cache_read_input_tokens : 0);
86-
const writeTokens = "cache_creation_input_tokens" in chunk.usage ? (chunk.usage as any).cache_creation_input_tokens : 0;
85+
const readTokens =
86+
cachedTokens ||
87+
("cache_read_input_tokens" in chunk.usage ? (chunk.usage as any).cache_read_input_tokens : 0)
88+
const writeTokens =
89+
"cache_creation_input_tokens" in chunk.usage ? (chunk.usage as any).cache_creation_input_tokens : 0
8790

8891
yield {
8992
type: "usage",

0 commit comments

Comments
 (0)