Skip to content

Commit 73184bc

Browse files
Dragon-GCSkraemahz
authored andcommitted
feat:convenient to access output text for response api (64bit#463)
1 parent ec25fbf commit 73184bc

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

async-openai/src/types/responses/response.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,6 +2346,36 @@ pub struct Response {
23462346
pub usage: Option<ResponseUsage>,
23472347
}
23482348

2349+
impl Response {
2350+
/// SDK-only convenience property that contains the aggregated text output from all
2351+
/// `output_text` items in the `output` array, if any are present.
2352+
pub fn output_text(&self) -> Option<String> {
2353+
let output = self
2354+
.output
2355+
.iter()
2356+
.filter_map(|item| match item {
2357+
OutputItem::Message(msg) => Some(
2358+
msg.content
2359+
.iter()
2360+
.filter_map(|content| match content {
2361+
OutputMessageContent::OutputText(ot) => Some(ot.text.clone()),
2362+
_ => None,
2363+
})
2364+
.collect::<Vec<String>>(),
2365+
),
2366+
_ => None,
2367+
})
2368+
.flatten()
2369+
.collect::<Vec<String>>()
2370+
.join("");
2371+
if output.is_empty() {
2372+
None
2373+
} else {
2374+
Some(output)
2375+
}
2376+
}
2377+
}
2378+
23492379
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
23502380
#[serde(rename_all = "snake_case")]
23512381
pub enum Status {

examples/responses/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
4444
println!("{}", serde_json::to_string(&request).unwrap());
4545

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

49+
println!("\nOutput Text: {output_text:?}\n",);
4850
for output in response.output {
4951
println!("\nOutput: {:?}\n", output);
5052
}

0 commit comments

Comments
 (0)