Skip to content

Enhance OpenAIError::StreamError payload #434

@coinmoles

Description

@coinmoles

Description

Currently, the OpenAIError::StreamErrorvariant only carries a String payload.

/// Error on SSE streaming
#[error("stream failed: {0}")]
StreamError(String),

This makes it hard to match the error, and when I log the error (even with '{:?}') it is hard to get a meaningful information for debugging.

Problem

If I send an invalid request to the server and receive an error. The response body often holds information about why the request was invalid. For example, if I set both max_tokens and max_completion_tokens, I get the error:

{
  "error": {
    "message": "Setting 'max_tokens' and 'max_completion_tokens' at the same time is not supported.",
    "type": "invalid_request_error",
    "param": "max_tokens",
    "code": "invalid_parameter_combination"
  }
}

But when I print the message, I get stream failed: Invalid status code: 400 Bad Request.

The only way to get the original error message is to use the verbose reqwest::Client and setting the log level for reqwest at trace. However, this is obviously not a good solution.

Proposed Solution

Right now, I could only find three places where this error variant is initialized:

Twice in Client::stream and Client::stream_mapped_raw_events, where the inner reqwest_eventsource::Error is converted into a String via to_string() call.

Err(e) => {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) {
// rx dropped
break;
}
}

Err(e) => {
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) {
// rx dropped
break;
}
}

And once in AssistantEventStream::try_from, where an unrecognized event (eventsource_stream::Event) is pretty-printed into a String

_ => Err(OpenAIError::StreamError(
"Unrecognized event: {value:?#}".into(),
)),

Therefore, it should be relative to define a new enum:

pub enum StreamErrorKind {
    #[error("{0}")]
    ReqwestEventSource(reqwest_eventsource::Error),
    #[error("Unrecognized event: {0:#?}")]
    UnrecognizedEvent(eventsource_stream::Event),
}

And change the StreamError payload to this type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions