open_ai: Fix error response parsing (#36390)
Closes #35925 Release Notes: - Fixed OpenAI error response parsing in some cases
This commit is contained in:
parent
61ce07a91b
commit
42ffa8900a
1 changed files with 7 additions and 7 deletions
|
@ -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: {}",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue