8000 Normalize provider message content extraction by pntech20 · Pull Request #32 · Mnehmos/LLM-Chess · GitHub
[go: up one dir, main page]

Skip to content

Normalize provider message content extraction - #32

Open
pntech20 wants to merge 1 commit into
Mnehmos:mainfrom
pntech20:codex/normalize-provider-content
Open

pntech20 wants to merge 1 commit into
Mnehmos:mainfrom
pntech20:codex/normalize-provider-content

Conversation

@pntech20
@pntech20 pntech20 commented May 17, 2026 •
Copy link
Copy Markdown

Summary

  • adds a shared message-content extractor for provider responses
  • reuses it in OpenAI, OpenRouter, and Ollama non-streaming response parsing
  • applies the same normalization to streamed content deltas and commentary response paths

Fixes #13.

Verification

  • npm run build
  • npx eslint src/llm/content.ts src/llm/openai.ts src/llm/openrouter.ts src/llm/ollama.ts
  • git diff --check

Note: full npm run lint still fails on existing unrelated React/hooks and unused-variable violations outside the LLM provider files.

Summary by CodeRabbit

  • Refactor
    • Unified message content extraction across Ollama, OpenAI, and OpenRouter integrations into a shared utility function.
    • Message content parsing now uses consistent normalization logic across all LLM providers.

Review Change Stack

@coderabbitai
coderabbitai Bot commented May 17, 2026 •
Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR introduces a shared content extraction utility and applies it across three LLM provider clients to normalize message content handling. A new extractMessageContent function handles strings, arrays, and structured objects; all three providers (OpenAI, Ollama, OpenRouter) then replace their fragmented extraction logic with unified calls to this utility.

Changes

Content extraction normalization

Layer / File(s) Summary
Content extraction utility contract
src/llm/content.ts
extractMessageContent and extractMessageContentPart normalize various input shapes into consistent string output, recursively concatenating arrays and extracting text/content fields from objects.
OpenAI provider migration
src/llm/openai.ts
Replaces local extractTextContent helper with shared extractMessageContent across all response paths: chat response content, streaming delta extraction, and commentary endpoints; removes redundant local function.
Ollama provider migration
src/llm/ollama.ts
Routes message content from non-streaming chat, streaming deltas, and commentary requests through extractMessageContent instead of direct field access.
OpenRouter provider migration
src/llm/openrouter.ts
Switches all `

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 A hopper's refactor, clean and bright,
Three providers dance to one unified rite—
No more scattered extraction, just shared purity!
One utility, three clients, extraction's maturity.
✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Normalize provider message content extraction' directly reflects the main objective of creating a shared content extraction utility and applying it consistently across all LLM provider implementations.
Linked Issues check ✅ Passed The PR successfully addresses all three acceptance criteria: creates a shared extractMessageContent utility, eliminates raw content assumptions in OpenRouter/OpenAI/Ollama, and ensures consistent LLMRawResponse semantics across providers.
Out of Scope Changes check ✅ Passed All changes are directly within scope: new utility module, imports in three provider files, and content extraction calls at expected response/streaming paths align with issue #13 requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/llm/openai.ts (1)

214-216: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Use isStreaming to select SSE parsing path

Line 214 checks onToken directly. If onToken is provided but shouldStream(model) is false, the request is JSON (stream: false) yet this branch still calls readStream, causing incorrect parsing.

Proposed fix
-    if (onToken && response.body) {
+    if (isStreaming && response.body) {
       return this.readStream(response.body, model, startTime, onToken);
     }

As per coding guidelines, "Fix LLM stream response parsing: check isStreaming flag instead of onToken callback to correctly distinguish JSON responses from SSE-formatted streams (critical bugs at openai.ts:189, openrouter.ts:179)."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/llm/openai.ts` around lines 214 - 216, The branch currently checks the
presence of the onToken callback and calls readStream(response.body, model,
startTime, onToken) which attempts SSE parsing even for non-stream JSON
responses; change the condition to use the isStreaming/shouldStream predicate
(e.g., shouldStream(model) or a computed isStreaming boolean used earlier)
instead of onToken so that readStream is only invoked when the request was made
as a stream; update the conditional that references response.body to guard with
isStreaming (and preserve the existing parameters to readStream) and ensure
non-stream paths handle JSON parsing as before.
src/llm/openrouter.ts (1)

231-234: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Gate streaming reader with isStreaming

Line 232 uses onToken directly. When streaming is disabled for a model (shouldStream(model) === false), this can still route a JSON response into readStream.

Proposed fix
-    if (onToken && response.body) {
+    if (isStreaming && response.body) {
       return this.readStream(response.body, model, startTime, onToken);
     }

As per coding guidelines, "Fix LLM stream response parsing: check isStreaming flag instead of onToken callback to correctly distinguish JSON responses from SSE-formatted streams (critical bugs at openai.ts:189, openrouter.ts:179)."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/llm/openrouter.ts` around lines 231 - 234, The streaming branch currently
checks only for onToken and can call readStream on non-SSE JSON responses;
change the condition to guard with the model streaming flag instead: compute or
use the existing isStreaming/shouldStream(model) boolean and replace the `if
(onToken && response.body)` with something like `if (isStreaming && onToken &&
response.body)` (or `if (shouldStream(model) && onToken && response.body)`), so
readStream(model, startTime, onToken) is only invoked for true streaming models
and JSON responses take the non-stream parsing path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/llm/openai.ts`:
- Around line 214-216: The branch currently checks the presence of the onToken
callback and calls readStream(response.body, model, startTime, onToken) which
attempts SSE parsing even for non-stream JSON responses; change the condition to
use the isStreaming/shouldStream predicate (e.g., shouldStream(model) or a
computed isStreaming boolean used earlier) instead of onToken so that readStream
is only invoked when the request was made as a stream; update the conditional
that references response.body to guard with isStreaming (and preserve the
existing parameters to readStream) and ensure non-stream paths handle JSON
parsing as before.

In `@src/llm/openrouter.ts`:
- Around line 231-234: The streaming branch currently checks only for onToken
and can call readStream on non-SSE JSON responses; change the condition to guard
with the model streaming flag instead: compute or use the existing
isStreaming/shouldStream(model) boolean and replace the `if (onToken &&
response.body)` with something like `if (isStreaming && onToken &&
response.body)` (or `if (shouldStream(model) && onToken && response.body)`), so
readStream(model, startTime, onToken) is only invoked for true streaming models
and JSON responses take the non-stream parsing path.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a37310ef-fed6-43ab-819b-b14c3883475e

📥 Commits

Reviewing files that changed from the base of the PR and between 37bdfe7 and 38cb068.

📒 Files selected for processing (4)
  • src/llm/content.ts
  • src/llm/ollama.ts
  • src/llm/openai.ts
  • src/llm/openrouter.ts

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.

Provider correctness: normalize content extraction across providers

1 participant

0