Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion async-openai/src/types/responses/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ pub struct InputTextContent {
#[builder(build_fn(error = "OpenAIError"))]
pub struct InputImageContent {
/// The detail level of the image to be sent to the model. One of `high`, `low`, or `auto`.
/// Defaults to `auto`.
/// Defaults to `auto` when omitted in JSON input.
#[serde(default)]
pub detail: ImageDetail,
/// The ID of the file to be sent to the model.
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
79 changes: 77 additions & 2 deletions async-openai/tests/responses_input_item_serde.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#![cfg(feature = "response-types")]

use async_openai::types::responses::{
EasyInputContent, InputItem, MessageType, Role, WebSearchApproximateLocation,
WebSearchApproximateLocationType,
EasyInputContent, ImageDetail, InputContent, InputItem, InputRole, Item, MessageItem,
MessageType, Role, WebSearchApproximateLocation, WebSearchApproximateLocationType,
};
use serde_json::json;

Expand Down Expand Up @@ -62,3 +62,78 @@ fn web_search_approximate_location_without_type_defaults_and_serializes_canonica
})
);
}

#[test]
fn input_item_easy_message_multimodal_without_detail_defaults_and_serializes_canonically() {
let input_item: InputItem = serde_json::from_value(json!({
"role": "user",
"content": [
{"type": "input_text", "text": "describe this"},
{"type": "input_image", "image_url": "https://example.com/cat.png"}
]
}))
.expect("deserialize easy input image without detail");

match &input_item {
InputItem::EasyMessage(msg) => {
assert_eq!(msg.r#type, MessageType::Message);
assert_eq!(msg.role, Role::User);
match &msg.content {
EasyInputContent::ContentList(parts) => {
assert_eq!(parts.len(), 2);
match &parts[1] {
InputContent::InputImage(img) => {
assert_eq!(img.detail, ImageDetail::Auto);
assert_eq!(
img.image_url.as_deref(),
Some("https://example.com/cat.png")
);
assert_eq!(img.file_id, None);
}
other => panic!("expected InputImage, got {other:?}"),
}
}
other => panic!("expected ContentList, got {other:?}"),
}
}
other => panic!("expected EasyMessage, got {other:?}"),
}

let serialized = serde_json::to_value(&input_item).expect("serialize input item");
assert_eq!(
serialized,
json!({
"type": "message",
"role": "user",
"content": [
{"type": "input_text", "text": "describe this"},
{"type": "input_image", "detail": "auto", "image_url": "https://example.com/cat.png"}
]
})
);
}

#[test]
fn input_item_strict_message_multimodal_without_detail_defaults() {
let input_item: InputItem = serde_json::from_value(json!({
"type": "message",
"role": "user",
"content": [
{"type": "input_image", "image_url": "https://example.com/cat.png"}
]
}))
.expect("deserialize strict input image without detail");

match &input_item {
InputItem::Item(Item::Message(MessageItem::Input(msg))) => {
assert_eq!(msg.role, InputRole::User);
match &msg.content[0] {
InputContent::InputImage(img) => {
assert_eq!(img.detail, ImageDetail::Auto);
}
other => panic!("expected InputImage, got {other:?}"),
}
}
other => panic!("expected Item::Message(Input), got {other:?}"),
}
}
Loading