Skip to content

Commit ed03e50

Browse files
Dragon-GCSifsheldon
authored andcommitted
feat:convenient to access output text for response api (64bit#463)
(cherry picked from commit 8e9639a)
1 parent 511af44 commit ed03e50

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
@@ -2347,6 +2347,36 @@ pub struct Response {
23472347
pub usage: Option<ResponseUsage>,
23482348
}
23492349

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