Skip to content

fix(sdk): decode SSE error body before formatting in stream client#4345

Open
CAN-Aidoo wants to merge 1 commit into
traceloop:mainfrom
CAN-Aidoo:fix/sdk-stream-client-decode
Open

fix(sdk): decode SSE error body before formatting in stream client#4345
CAN-Aidoo wants to merge 1 commit into
traceloop:mainfrom
CAN-Aidoo:fix/sdk-stream-client-decode

Conversation

@CAN-Aidoo

@CAN-Aidoo CAN-Aidoo commented Jul 2, 2026

Copy link
Copy Markdown

Summary

Decode the bytes body in SSEClient._handle_sse_response before interpolating it into the error message, using .decode(errors=\"replace\") to stay safe on non-UTF-8 responses. Drops the acknowledging # TODO, the # type: ignore[str-bytes-safe], and the # noqa: E501.

Motivation

response.aread() returns bytes, so the previous f-string rendered the body as b'{\"error\":...}' — leading b, surrounding quotes, and \xNN escapes for any non-ASCII byte. The success path two lines below already does response_text.decode(); this PR reuses that pattern for the error path. The existing # TODO: Fix bytes formatting - should decode error_text or use !r in the file called this out.

Test plan

  • New tests/evaluator/test_stream_client.py covers the happy path (readable decoded body in the raised exception, no b' artifact) and non-UTF-8 input (does not raise on decode).
  • uv run ruff check passes on the modified files.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error messages for failed streaming responses so response bodies are shown as readable text instead of raw bytes.
    • Ensured invalid text in error responses no longer breaks error handling; the HTTP status code is still included.
  • Tests

    • Added coverage for error-response handling, including valid text bodies and invalid UTF-8 cases.

SSEClient._handle_sse_response formatted the raw bytes body straight into the error message, rendering it as b'...' with escaped byte sequences and requiring a type: ignore[str-bytes-safe] and noqa: E501 to suppress the warning (the existing TODO in the file called this out). Decode with errors="replace" so non-UTF-8 responses still surface a readable message, and drop the ignore comments.
@CLAassistant

CLAassistant commented Jul 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@coderabbitai

coderabbitai Bot commented Jul 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f61520e1-8779-4c5b-b3bd-40d281f54e4b

📥 Commits

Reviewing files that changed from the base of the PR and between 93429cf and e1a073c.

📒 Files selected for processing (2)
  • packages/traceloop-sdk/tests/evaluator/test_stream_client.py
  • packages/traceloop-sdk/traceloop/sdk/evaluator/stream_client.py

📝 Walkthrough

Walkthrough

The SSE error-handling path in SSEClient._handle_sse_response now decodes the error response body bytes to a string using errors="replace" before constructing the error message, replacing prior bytes-based formatting. New async tests validate decoded exception messages for both valid and invalid UTF-8 error bodies.

Changes

SSE Error Body Decoding

Layer / File(s) Summary
Decode error body in SSEClient
packages/traceloop-sdk/traceloop/sdk/evaluator/stream_client.py
Error body bytes are decoded to a string with errors="replace" before being used in the constructed error message, removing the prior bytes-based approach and its TODO comment.
Tests for decoded error messages
packages/traceloop-sdk/tests/evaluator/test_stream_client.py
Adds a _FakeResponse helper and two tests verifying that valid UTF-8 JSON error bodies are decoded into exception messages and invalid UTF-8 bodies still raise exceptions containing the status code.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 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 clearly summarizes the main change: decoding SSE error bodies before formatting them in the stream client.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ 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.

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