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
30 changes: 30 additions & 0 deletions async-openai/src/types/responses/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,36 @@ pub struct Response {
pub usage: Option<ResponseUsage>,
}

impl Response {
/// SDK-only convenience property that contains the aggregated text output from all
/// `output_text` items in the `output` array, if any are present.
pub fn output_text(&self) -> Option<String> {
let output = self
.output
.iter()
.filter_map(|item| match item {
OutputItem::Message(msg) => Some(
msg.content
.iter()
.filter_map(|content| match content {
OutputMessageContent::OutputText(ot) => Some(ot.text.clone()),
_ => None,
})
.collect::<Vec<String>>(),
),
_ => None,
})
.flatten()
.collect::<Vec<String>>()
.join("");
if output.is_empty() {
None
} else {
Some(output)
}
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Status {
Expand Down
2 changes: 2 additions & 0 deletions examples/responses/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
println!("{}", serde_json::to_string(&request).unwrap());

let response = client.responses().create(request).await?;
let output_text = response.output_text().unwrap_or("Empty text output".into());

println!("\nOutput Text: {output_text:?}\n",);
for output in response.output {
println!("\nOutput: {:?}\n", output);
}
Expand Down
Loading