pub struct ChatResponse {
pub id: String,
pub model: String,
pub content: Vec<ContentBlock>,
pub usage: Option<ChatUsage>,
pub stop_reason: String,
pub citations: Vec<Citation>,
pub cost_ticks: i64,
pub request_id: String,
}Expand description
Response from a non-streaming chat request.
Fields§
§id: StringUnique request identifier.
model: StringModel that generated the response.
content: Vec<ContentBlock>List of content blocks (text, thinking, tool_use).
usage: Option<ChatUsage>Token counts and cost.
stop_reason: StringWhy generation stopped (“end_turn”, “tool_use”, “max_tokens”).
citations: Vec<Citation>Citations from web search (when search is enabled via provider_options).
cost_ticks: i64Total cost from the X-QAI-Cost-Ticks header.
request_id: StringFrom the X-QAI-Request-Id header.
Implementations§
Source§impl ChatResponse
impl ChatResponse
Sourcepub fn text(&self) -> String
pub fn text(&self) -> String
Returns the concatenated text content, ignoring thinking and tool_use blocks.
Examples found in repository?
examples/chat.rs (line 32)
12async fn main() {
13 let api_key = std::env::var("QAI_API_KEY").expect("QAI_API_KEY environment variable is required");
14
15 let client = Client::new(api_key);
16
17 // --- Non-streaming example ---
18 println!("=== Non-streaming Chat ===");
19
20 let resp = client
21 .chat(&ChatRequest {
22 model: "claude-sonnet-4-6".into(),
23 messages: vec![ChatMessage::user(
24 "What is quantum computing in one sentence?",
25 )],
26 ..Default::default()
27 })
28 .await
29 .expect("Chat failed");
30
31 println!("Model: {}", resp.model);
32 println!("Response: {}", resp.text());
33 if let Some(usage) = &resp.usage {
34 println!(
35 "Tokens: {} in / {} out (cost: {} ticks)",
36 usage.input_tokens, usage.output_tokens, usage.cost_ticks
37 );
38 }
39 println!("Request ID: {}\n", resp.request_id);
40
41 // --- Streaming example ---
42 println!("=== Streaming Chat ===");
43
44 let mut stream = client
45 .chat_stream(&ChatRequest {
46 model: "claude-sonnet-4-6".into(),
47 messages: vec![ChatMessage::user(
48 "Count from 1 to 5, one number per line.",
49 )],
50 ..Default::default()
51 })
52 .await
53 .expect("ChatStream failed");
54
55 while let Some(ev) = stream.next().await {
56 match ev.event_type.as_str() {
57 "content_delta" => {
58 if let Some(delta) = &ev.delta {
59 print!("{}", delta.text);
60 }
61 }
62 "usage" => {
63 if let Some(usage) = &ev.usage {
64 println!("\n[Cost: {} ticks]", usage.cost_ticks);
65 }
66 }
67 "error" => {
68 eprintln!("Stream error: {}", ev.error.as_deref().unwrap_or("unknown"));
69 std::process::exit(1);
70 }
71 "done" => {
72 println!("\n[Stream complete]");
73 }
74 _ => {}
75 }
76 }
77}Sourcepub fn tool_calls(&self) -> Vec<&ContentBlock>
pub fn tool_calls(&self) -> Vec<&ContentBlock>
Returns all tool_use blocks from the response.
Trait Implementations§
Source§impl Clone for ChatResponse
impl Clone for ChatResponse
Source§fn clone(&self) -> ChatResponse
fn clone(&self) -> ChatResponse
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ChatResponse
impl Debug for ChatResponse
Source§impl<'de> Deserialize<'de> for ChatResponse
impl<'de> Deserialize<'de> for ChatResponse
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations§
impl Freeze for ChatResponse
impl RefUnwindSafe for ChatResponse
impl Send for ChatResponse
impl Sync for ChatResponse
impl Unpin for ChatResponse
impl UnsafeUnpin for ChatResponse
impl UnwindSafe for ChatResponse
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more