open_ai: Fix error response parsing (#36390)

Closes #35925

Release Notes:

- Fixed OpenAI error response parsing in some cases
This commit is contained in:
Oleksiy Syvokon 2025-08-18 11:54:31 +03:00 committed by GitHub
parent 61ce07a91b
commit 42ffa8900a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -432,11 +432,16 @@ pub struct ChoiceDelta {
pub finish_reason: Option<String>,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct OpenAiError {
message: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ResponseStreamResult {
Ok(ResponseStreamEvent),
Err { error: String },
Err { error: OpenAiError },
}
#[derive(Serialize, Deserialize, Debug)]
@ -475,7 +480,7 @@ pub async fn stream_completion(
match serde_json::from_str(line) {
Ok(ResponseStreamResult::Ok(response)) => Some(Ok(response)),
Ok(ResponseStreamResult::Err { error }) => {
Some(Err(anyhow!(error)))
Some(Err(anyhow!(error.message)))
}
Err(error) => {
log::error!(
@ -502,11 +507,6 @@ pub async fn stream_completion(
error: OpenAiError,
}
#[derive(Deserialize)]
struct OpenAiError {
message: String,
}
match serde_json::from_str::<OpenAiResponse>(&body) {
Ok(response) if !response.error.message.is_empty() => Err(anyhow!(
"API request to {} failed: {}",