Skip to content

Cancelling streaming returns error "error, <nil>" #1060

@OrkhanAlikhanov

Description

@OrkhanAlikhanov

Describe the bug
After cancelling the context passed to CreateChatCompletionStream, stream.Recv() returns "error, " error. Debugging shows that it happens in these lines:

for {
rawLine, readErr := stream.reader.ReadBytes('\n')
if readErr != nil || hasErrorPrefix {
respErr := stream.unmarshalError()
if respErr != nil {
return nil, fmt.Errorf("error, %w", respErr.Error)
}
return nil, readErr
}

After cancellation rawLine, readErr := stream.reader.ReadBytes('\n') returns io.EOF error but then stream.unmarshalError() returns *openai.ErrorResponse that wraps *openai.APIError which wraps nil value.

Image

Expected behavior
It should propagate io.EOF properly

Screenshots/Logs
If applicable, add screenshots to help explain your problem. For non-graphical issues, please provide any relevant logs or stack traces.

Environment (please complete the following information):

  • go-openai version: v1.40.1
  • Go version: 1.24.5
  • OpenAI API version: v1
  • OS: macOS

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions