Skip to content

Commit 0ce86b0

Browse files
committed
quick fix for OpenAI
1 parent 4c2f22e commit 0ce86b0

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

llm/openai/src/client.rs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use golem_llm::error::{error_code_from_status, from_event_source_error, from_reqwest_error};
22
use golem_llm::event_source::EventSource;
3-
use golem_llm::golem::llm::llm::Error;
3+
use golem_llm::golem::llm::llm::{Error, ErrorCode};
44
use log::trace;
55
use reqwest::header::HeaderValue;
66
use reqwest::{Client, Method, Response};
@@ -97,7 +97,7 @@ pub struct CreateModelResponseResponse {
9797
pub error: Option<ErrorObject>,
9898
pub incomplete_details: Option<IncompleteDetailsObject>,
9999
pub status: Status,
100-
pub output: Vec<OutputItem>,
100+
pub output: Vec<OpenOutputItem>,
101101
pub usage: Option<Usage>,
102102
pub metadata: Option<serde_json::Value>,
103103
}
@@ -122,6 +122,13 @@ pub enum OutputItem {
122122
},
123123
}
124124

125+
#[derive(Debug, Clone, Serialize, Deserialize)]
126+
#[serde(untagged)]
127+
pub enum OpenOutputItem {
128+
Known(OutputItem),
129+
Other(serde_json::Value),
130+
}
131+
125132
#[derive(Debug, Clone, Serialize, Deserialize)]
126133
#[serde(tag = "type")]
127134
pub enum OutputMessageContent {
@@ -273,9 +280,15 @@ pub struct ResponseOutputItemDone {
273280
fn parse_response<T: DeserializeOwned + Debug>(response: Response) -> Result<T, Error> {
274281
let status = response.status();
275282
if status.is_success() {
276-
let body = response
277-
.json::<T>()
278-
.map_err(|err| from_reqwest_error("Failed to decode response body", err))?;
283+
let body_text = response
284+
.text()
285+
.map_err(|err| from_reqwest_error("Failed to receive response body", err))?;
286+
287+
let body = serde_json::from_str::<T>(&body_text).map_err(|err| Error {
288+
code: ErrorCode::InvalidRequest,
289+
message: format!("Failed to decode response body: {}", err),
290+
provider_error_json: Some(body_text),
291+
})?;
279292

280293
trace!("Received response from OpenAI API: {body:?}");
281294

llm/openai/src/conversions.rs

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::client::{
22
CreateModelResponseRequest, CreateModelResponseResponse, Detail, InnerInput, InnerInputItem,
3-
Input, InputItem, OutputItem, OutputMessageContent, Tool,
3+
Input, InputItem, OpenOutputItem, OutputItem, OutputMessageContent, Tool,
44
};
55
use base64::{engine::general_purpose, Engine as _};
66
use golem_llm::error::error_code_from_status;
77
use golem_llm::golem::llm::llm::{
88
Config, ContentPart, Error, ErrorCode, Event, ImageDetail, ImageReference, Message, Response,
99
ResponseMetadata, Role, ToolCall, ToolDefinition, ToolResult, Usage,
1010
};
11+
use log::trace;
1112
use reqwest::StatusCode;
1213
use std::collections::HashMap;
1314
use std::str::FromStr;
@@ -221,30 +222,35 @@ pub fn process_model_response(response: CreateModelResponseResponse) -> Result<R
221222

222223
for output_item in response.output {
223224
match output_item {
224-
OutputItem::Message { content, .. } => {
225-
for content in content {
226-
match content {
227-
OutputMessageContent::Text { text, .. } => {
228-
contents.push(ContentPart::Text(text));
229-
}
230-
OutputMessageContent::Refusal { refusal, .. } => {
231-
contents.push(ContentPart::Text(format!("Refusal: {refusal}")));
225+
OpenOutputItem::Known(output_item) => match output_item {
226+
OutputItem::Message { content, .. } => {
227+
for content in content {
228+
match content {
229+
OutputMessageContent::Text { text, .. } => {
230+
contents.push(ContentPart::Text(text));
231+
}
232+
OutputMessageContent::Refusal { refusal, .. } => {
233+
contents.push(ContentPart::Text(format!("Refusal: {refusal}")));
234+
}
232235
}
233236
}
234237
}
235-
}
236-
OutputItem::ToolCall {
237-
arguments,
238-
call_id,
239-
name,
240-
..
241-
} => {
242-
let tool_call = ToolCall {
243-
id: call_id,
238+
OutputItem::ToolCall {
239+
arguments,
240+
call_id,
244241
name,
245-
arguments_json: arguments,
246-
};
247-
tool_calls.push(tool_call);
242+
..
243+
} => {
244+
let tool_call = ToolCall {
245+
id: call_id,
246+
name,
247+
arguments_json: arguments,
248+
};
249+
tool_calls.push(tool_call);
250+
}
251+
},
252+
OpenOutputItem::Other(value) => {
253+
trace!("Ignoring unknown output item: {value:?}");
248254
}
249255
}
250256
}

0 commit comments

Comments
 (0)