Skip to content

Conversation

@hassan123789
Copy link

## Summary

Fixes #1060

When streaming is cancelled (e.g., via context cancellation), `stream.Recv()` returns `"error, <nil>"` instead of propagating the original read error (like `io.EOF`).

## Root Cause

In `stream_reader.go` line 66-68, when `ReadBytes('\n')` returns an error (like `io.EOF` on context cancellation), `unmarshalError()` is called. If the error accumulator contains an empty JSON object `{}`, it unmarshals to `&ErrorResponse{Error: nil}`. Since `respErr != nil` is true (the struct pointer exists), but `respErr.Error` is nil, `fmt.Errorf("error, %w", respErr.Error)` produces `"error, <nil>"`.

## Solution

Add a nil check for `respErr.Error`:

```go
if respErr != nil && respErr.Error != nil {
    return nil, fmt.Errorf("error, %w", respErr.Error)
}

This ensures the original read error is properly propagated when there's no actual API error to report.

Changes

  • stream_reader.go: Added nil check for respErr.Error before formatting error message
  • stream_reader_test.go: Added test case TestStreamReaderReturnsReadErrWhenErrorResponseHasNilError to verify the fix

Testing

All existing tests pass, and the new test verifies that io.EOF is properly returned instead of "error, <nil>".

When streaming is cancelled (e.g., via context cancellation),
ReadBytes returns io.EOF but unmarshalError() may return an
ErrorResponse with a nil Error field (from unmarshaling empty
JSON). This caused fmt.Errorf to produce 'error, <nil>' instead
of propagating the original read error.

This fix adds a nil check for respErr.Error before formatting
the error message.

Fixes sashabaranov#1060
@codecov
Copy link

codecov bot commented Dec 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.61%. Comparing base (5d7a276) to head (09600b0).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1096      +/-   ##
==========================================
+ Coverage   99.59%   99.61%   +0.02%     
==========================================
  Files          34       34              
  Lines        2209     1841     -368     
==========================================
- Hits         2200     1834     -366     
+ Misses          6        4       -2     
  Partials        3        3              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant