-
Notifications
You must be signed in to change notification settings - Fork 322
Description
Description
Currently, the OpenAIError::StreamError
variant only carries a String
payload.
async-openai/async-openai/src/error.rs
Lines 21 to 23 in 7964f86
/// 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.
async-openai/async-openai/src/client.rs
Lines 487 to 492 in 7964f86
Err(e) => { | |
if let Err(_e) = tx.send(Err(OpenAIError::StreamError(e.to_string()))) { | |
// rx dropped | |
break; | |
} | |
} |
async-openai/async-openai/src/client.rs
Lines 532 to 537 in 7964f86
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
async-openai/async-openai/src/types/assistant_stream.rs
Lines 210 to 212 in 7964f86
_ => 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.