From 99d9cd00ecba894929cee9084abed034491fbd80 Mon Sep 17 00:00:00 2001 From: uinstinct <61635505+uinstinct@users.noreply.github.com> Date: Fri, 17 Oct 2025 07:59:31 +0530 Subject: [PATCH] do not split lines again in processgeminiresponse --- core/llm/llms/Gemini.ts | 116 +++++++++----------- core/llm/llms/VertexAI.ts | 2 +- packages/openai-adapters/src/apis/Gemini.ts | 108 ++++++++---------- 3 files changed, 95 insertions(+), 131 deletions(-) diff --git a/core/llm/llms/Gemini.ts b/core/llm/llms/Gemini.ts index ab83eeba8..40b0a656e 100644 --- a/core/llm/llms/Gemini.ts +++ b/core/llm/llms/Gemini.ts @@ -312,75 +312,57 @@ class Gemini extends BaseLLM { } public async *processGeminiResponse( - stream: AsyncIterable, + response: Response, ): AsyncGenerator { - let buffer = ""; - for await (const chunk of stream) { - buffer += chunk; - - const parts = buffer.split("\n,"); - - let foundIncomplete = false; - for (let i = 0; i < parts.length; i++) { - const part = parts[i]; - let data: GeminiChatResponse; - try { - data = JSON.parse(part) as GeminiChatResponse; - } catch (e) { - foundIncomplete = true; - continue; // yo! - } - - if ("error" in data) { - throw new Error(data.error.message); - } - - // In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec - const contentParts = data?.candidates?.[0]?.content?.parts; - if (contentParts) { - const textParts: MessagePart[] = []; - const toolCalls: ToolCallDelta[] = []; - - for (const part of contentParts) { - if ("text" in part) { - textParts.push({ type: "text", text: part.text }); - } else if ("functionCall" in part) { - toolCalls.push({ - type: "function", - id: part.functionCall.id ?? uuidv4(), - function: { - name: part.functionCall.name, - arguments: - typeof part.functionCall.args === "string" - ? part.functionCall.args - : JSON.stringify(part.functionCall.args), - }, - }); - } else { - // Note: function responses shouldn't be streamed, images not supported - console.warn("Unsupported gemini part type received", part); - } - } - - const assistantMessage: AssistantChatMessage = { - role: "assistant", - content: textParts.length ? textParts : "", - }; - if (toolCalls.length > 0) { - assistantMessage.toolCalls = toolCalls; - } - if (textParts.length || toolCalls.length) { - yield assistantMessage; - } - } else { - // Handle the case where the expected data structure is not found - console.warn("Unexpected response format:", data); - } + for await (const chunk of streamSse(response)) { + let data: GeminiChatResponse; + try { + data = JSON.parse(chunk) as GeminiChatResponse; + } catch (e) { + continue; } - if (foundIncomplete) { - buffer = parts[parts.length - 1]; + + if ("error" in data) { + throw new Error(data.error.message); + } + + const contentParts = data?.candidates?.[0]?.content?.parts; + if (contentParts) { + const textParts: MessagePart[] = []; + const toolCalls: ToolCallDelta[] = []; + + for (const part of contentParts) { + if ("text" in part) { + textParts.push({ type: "text", text: part.text }); + } else if ("functionCall" in part) { + toolCalls.push({ + type: "function", + id: part.functionCall.id ?? uuidv4(), + function: { + name: part.functionCall.name, + arguments: + typeof part.functionCall.args === "string" + ? part.functionCall.args + : JSON.stringify(part.functionCall.args), + }, + }); + } else { + console.warn("Unsupported gemini part type received", part); + } + } + + const assistantMessage: AssistantChatMessage = { + role: "assistant", + content: textParts.length ? textParts : "", + }; + if (toolCalls.length > 0) { + assistantMessage.toolCalls = toolCalls; + } + if (textParts.length || toolCalls.length) { + yield assistantMessage; + } } else { - buffer = ""; + console.warn("Unexpected response format:", data); } } } @@ -406,7 +388,7 @@ class Gemini extends BaseLLM { signal, }); - for await (const chunk of this.processGeminiResponse(streamSse(response))) { + for await (const chunk of this.processGeminiResponse(response)) { yield chunk; } } diff --git a/core/llm/llms/VertexAI.ts b/core/llm/llms/VertexAI.ts index 216ed03cb..97600e054 100644 --- a/core/llm/llms/VertexAI.ts +++ b/core/llm/llms/VertexAI.ts @@ -287,7 +287,7 @@ class VertexAI extends BaseLLM { body: JSON.stringify(body), signal, }); - yield* this.geminiInstance.processGeminiResponse(streamSse(response)); + yield* this.geminiInstance.processGeminiResponse(response); } private async *streamChatBison( diff --git a/packages/openai-adapters/src/apis/Gemini.ts b/packages/openai-adapters/src/apis/Gemini.ts index 734c32e86..64d926f1b 100644 --- a/packages/openai-adapters/src/apis/Gemini.ts +++ b/packages/openai-adapters/src/apis/Gemini.ts @@ -284,76 +284,58 @@ export class GeminiApi implements BaseLlmApi { } async *handleStreamResponse(response: any, model: string) { - let buffer = ""; let usage: UsageInfo | undefined = undefined; for await (const chunk of streamSse(response as any)) { - buffer += chunk; - - const parts = buffer.split("\n,"); - - let foundIncomplete = false; - for (let i = 0; i < parts.length; i++) { - const part = parts[i]; - let data; - try { - data = JSON.parse(part); - } catch (e) { - foundIncomplete = true; - continue; // yo! - } - if (data.error) { - throw new Error(data.error.message); - } - - // Check for usage metadata - if (data.usageMetadata) { - usage = { - prompt_tokens: data.usageMetadata.promptTokenCount || 0, - completion_tokens: data.usageMetadata.candidatesTokenCount || 0, - total_tokens: data.usageMetadata.totalTokenCount || 0, - }; - } - - // In case of max tokens reached, gemini will sometimes return content with no parts, even though that doesn't match the API spec - const contentParts = data?.candidates?.[0]?.content?.parts; - if (contentParts) { - for (const part of contentParts) { - if ("text" in part) { - yield chatChunk({ - content: part.text, - model, - }); - } else if ("functionCall" in part) { - yield chatChunkFromDelta({ - model, - delta: { - tool_calls: [ - { - index: 0, - id: part.functionCall.id ?? uuidv4(), - type: "function", - function: { - name: part.functionCall.name, - arguments: JSON.stringify(part.functionCall.args), - }, - }, - ], - }, - }); - } - } - } else { - console.warn("Unexpected response format:", data); - } + let data; + try { + data = JSON.parse(chunk); + } catch (e) { + continue; } - if (foundIncomplete) { - buffer = parts[parts.length - 1]; + if (data.error) { + throw new Error(data.error.message); + } + + if (data.usageMetadata) { + usage = { + prompt_tokens: data.usageMetadata.promptTokenCount || 0, + completion_tokens: data.usageMetadata.candidatesTokenCount || 0, + total_tokens: data.usageMetadata.totalTokenCount || 0, + }; + } + + const contentParts = data?.candidates?.[0]?.content?.parts; + if (contentParts) { + for (const part of contentParts) { + if ("text" in part) { + yield chatChunk({ + content: part.text, + model, + }); + } else if ("functionCall" in part) { + yield chatChunkFromDelta({ + model, + delta: { + tool_calls: [ + { + index: 0, + id: part.functionCall.id ?? uuidv4(), + type: "function", + function: { + name: part.functionCall.name, + arguments: JSON.stringify(part.functionCall.args), + }, + }, + ], + }, + }); + } + } } else { - buffer = ""; + console.warn("Unexpected response format:", data); } } - // Emit usage at the end if we have it if (usage) { yield usageChatChunk({ model,