Skip to content

fix(llm-proxy): avoid disturbing response body when JSON parse fails#1923

Merged
chrarnoldus merged 1 commit intomainfrom
fix/response-body-disturbed
Apr 2, 2026
Merged

fix(llm-proxy): avoid disturbing response body when JSON parse fails#1923
chrarnoldus merged 1 commit intomainfrom
fix/response-body-disturbed

Conversation

@kilo-code-bot
Copy link
Copy Markdown
Contributor

@kilo-code-bot kilo-code-bot bot commented Apr 2, 2026

Summary

Fix TypeError: Response body object should not be disturbed or locked (KILOCODE-WEB-1PNQ) in the LLM proxy rewrite layer. The three rewriteFreeModelResponse_* functions used response.clone().json() to attempt JSON parsing, falling back to passing response.body through on failure. When .json() failed on the clone, the original response body was left in a disturbed/locked state, making it unusable in the catch block.

The fix reads the body once via response.text() and then uses JSON.parse(). On parse failure, the already-read text string is passed through instead of the now-consumed body stream.

Verification

  • pnpm typecheck passes (exit code 0)
  • pnpm format:check passes
  • Lint passes (0 warnings, 0 errors)

Visual Changes

N/A

Reviewer Notes

The same pattern was fixed in all three functions: rewriteFreeModelResponse_ChatCompletions, rewriteFreeModelResponse_Messages, and rewriteFreeModelResponse_Responses. The Sentry issue only showed the ChatCompletions path, but the other two had the identical bug.

Read body text once via response.text() instead of using
response.clone().json(). When the cloned response's .json() call
failed (e.g. empty/invalid body), the original response.body stream
was left in a disturbed/locked state, causing 'Response body object
should not be disturbed or locked' TypeError.

Fixes KILOCODE-WEB-1PNQ
@kilo-code-bot
Copy link
Copy Markdown
Contributor Author

kilo-code-bot bot commented Apr 2, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (1 files)
  • src/lib/rewriteModelResponse.ts

Reviewed by gpt-5.4-2026-03-05 · 199,076 tokens

@chrarnoldus
Copy link
Copy Markdown
Contributor

@chrarnoldus chrarnoldus merged commit 8d86463 into main Apr 2, 2026
15 checks passed
@chrarnoldus chrarnoldus deleted the fix/response-body-disturbed branch April 2, 2026 17:10
jeanduplessis pushed a commit that referenced this pull request Apr 2, 2026
…1923)

Read body text once via response.text() instead of using
response.clone().json(). When the cloned response's .json() call
failed (e.g. empty/invalid body), the original response.body stream
was left in a disturbed/locked state, causing 'Response body object
should not be disturbed or locked' TypeError.

Fixes KILOCODE-WEB-1PNQ

Co-authored-by: kiloconnect[bot] <240665456+kiloconnect[bot]@users.noreply.github.com>
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.

2 participants