Skip to main content

agent_framework_openai/
lib.rs

1//! # agent-framework-openai
2//!
3//! An OpenAI (and OpenAI-compatible) [`ChatClient`] for `agent-framework-rs`.
4//!
5//! Works against the OpenAI Chat Completions API and any compatible endpoint
6//! (Azure OpenAI, Ollama, together.ai, local servers, …) by overriding the
7//! base URL.
8//!
9//! ```no_run
10//! use agent_framework_openai::OpenAIChatCompletionClient;
11//! use agent_framework_core::prelude::*;
12//!
13//! # async fn demo() -> Result<()> {
14//! let client = OpenAIChatCompletionClient::new("sk-...", "gpt-4o-mini");
15//! let agent = Agent::builder(client)
16//!     .instructions("You are concise.")
17//!     .build();
18//! let reply = agent.run_once("Say hi").await?;
19//! println!("{}", reply.text());
20//! # Ok(())
21//! # }
22//! ```
23
24/// Conversion between framework types and the OpenAI chat-completions wire
25/// format.
26///
27/// This module is public (but hidden from docs) so that other
28/// OpenAI-wire-compatible clients in this workspace — currently
29/// `agent-framework-azure` — can reuse request/response conversion instead of
30/// duplicating it. It is not intended as a stable external API.
31#[doc(hidden)]
32pub mod convert;
33pub mod embeddings;
34
35pub mod responses;
36pub use embeddings::OpenAIEmbeddingClient;
37pub use responses::OpenAIChatClient;
38
39use std::collections::{HashMap, VecDeque};
40use std::sync::Arc;
41
42use agent_framework_core::client::{ChatClient, ChatStream};
43use agent_framework_core::error::{ContentFilterCategory, ContentFilterDetail, Error, Result};
44use agent_framework_core::streaming::Utf8StreamDecoder;
45use agent_framework_core::types::{
46    ChatOptions, ChatResponse, ChatResponseUpdate, Content, FinishReason, FunctionArguments,
47    FunctionCallContent, Message, Role, TextContent, UsageContent,
48};
49use futures::StreamExt;
50use serde_json::{json, Map, Value};
51
52pub(crate) const DEFAULT_BASE_URL: &str = "https://api.openai.com/v1";
53
54/// Parse a `Retry-After` header into a delay in seconds.
55///
56/// HTTP allows either a delay in seconds or an HTTP-date; OpenAI (and other
57/// rate limiters) use the integer/decimal-seconds form for `429`/`503`, which
58/// is what we honor. A date-form or unparseable value is treated as absent.
59/// Shared with [`responses`](crate::responses) so both OpenAI clients attach
60/// the same retry hint to [`Error::ServiceStatus`].
61pub(crate) fn parse_retry_after(headers: &reqwest::header::HeaderMap) -> Option<f64> {
62    headers
63        .get(reqwest::header::RETRY_AFTER)
64        .and_then(|v| v.to_str().ok())
65        .and_then(|s| s.trim().parse::<f64>().ok())
66        .filter(|s| s.is_finite() && *s >= 0.0)
67}
68
69/// Classify a non-success OpenAI-wire (Chat Completions / Responses) HTTP
70/// response into a granular [`Error`].
71///
72/// The single point of truth for status/body interpretation, used by every
73/// endpoint in this crate (`OpenAIChatCompletionClient::post` and [`responses`]) and reused
74/// by `agent-framework-azure` (Azure OpenAI is
75/// wire-compatible for Chat Completions and Responses), so the two stay
76/// identical rather than drifting.
77///
78/// Mirrors upstream's `openai/_chat_client.py` / `_responses_client.py`:
79///
80/// ```text
81/// except BadRequestError as ex:
82///     if ex.code == "content_filter":
83///         raise OpenAIContentFilterException(...)
84///     raise ServiceResponseException(...)
85/// ```
86///
87/// for the content-filter case, and extends upstream's exception
88/// *hierarchy* — `ServiceInvalidAuthError` / `ServiceInvalidRequestError`
89/// already exist in `agent_framework.exceptions`, even though today's Python
90/// OpenAI client folds every other status (auth failures included) into that
91/// same generic `ServiceResponseException` — to also classify by HTTP status:
92///
93/// * `401` / `403` -> [`Error::ServiceInvalidAuth`]
94/// * `400` / `404` / `422` -> [`Error::ServiceInvalidRequest`], unless the
95///   body signals a content-filter refusal (`error.code` or `error.type` ==
96///   `"content_filter"`, checked at the top level and inside a nested
97///   `innererror` for Azure OpenAI's shape) -> [`Error::ServiceContentFilter`]
98/// * anything else — notably `408` / `429` / `5xx`, which
99///   [`RetryOn::Default`](agent_framework_core::client::RetryOn::Default)
100///   depends on — -> [`Error::ServiceStatus`], unchanged
101///
102/// `body` is parsed leniently as JSON purely to look for the content-filter
103/// marker; a non-JSON or differently-shaped body just skips that check and
104/// falls through to the plain status-based classification, so this never
105/// panics or itself errors. `message` is used verbatim as the resulting
106/// error's text (callers already format a provider-specific "OpenAI API
107/// error 400: ..." string; this only picks the variant).
108pub fn classify_service_error(
109    status: u16,
110    body: &str,
111    message: impl Into<String>,
112    retry_after: Option<f64>,
113) -> Error {
114    let message = message.into();
115    match status {
116        401 | 403 => Error::service_invalid_auth(message),
117        400 | 404 | 422 => {
118            if body_signals_content_filter(body) {
119                match parse_content_filter_detail(body) {
120                    Some(detail) => Error::service_content_filter_with_detail(message, detail),
121                    None => Error::service_content_filter(message),
122                }
123            } else {
124                Error::service_invalid_request(message)
125            }
126        }
127        _ => Error::service_status(status, message, retry_after),
128    }
129}
130
131/// Whether an OpenAI/Azure-OpenAI-shaped error body signals a content-filter
132/// refusal.
133///
134/// Checks `error.code` — the field the OpenAI Python SDK's
135/// `BadRequestError.code` reads, and what upstream compares against
136/// `"content_filter"` — tolerantly falling back to `error.type` and to Azure
137/// OpenAI's nested `error.innererror` (`code`/`type`), since a
138/// `"content_filter"` marker has been observed in slightly different spots
139/// across OpenAI-wire-compatible providers. A non-JSON or unrecognized body
140/// shape is treated as "not a content filter" rather than erroring.
141fn body_signals_content_filter(body: &str) -> bool {
142    let Ok(value) = serde_json::from_str::<Value>(body) else {
143        return false;
144    };
145    let error = value.get("error").unwrap_or(&value);
146    is_content_filter_marker(error)
147        || error
148            .get("innererror")
149            .is_some_and(is_content_filter_marker)
150}
151
152/// Read Azure OpenAI's content-filter breakdown out of an error body.
153///
154/// Azure answers a filtered request with a nested `innererror` naming the
155/// policy that fired (`ResponsibleAIPolicyViolation`, `ContentFiltered`) and a
156/// `content_filter_result` map of category verdicts. Without it the caller has
157/// a message string and cannot tell a self-harm block from a jailbreak
158/// detection, or an input block from an output one — the distinctions an
159/// application acts on.
160///
161/// Returns `None` when the body carries nothing beyond the marker, so a plain
162/// OpenAI content-filter refusal stays a plain error rather than growing an
163/// empty detail. Every value is copied through verbatim: unlike upstream's
164/// enums, a code or severity Azure has not shipped yet arrives as itself
165/// instead of raising while classifying an error (upstream #8393 fixed that
166/// for the code; its severity parse still has it).
167fn parse_content_filter_detail(body: &str) -> Option<ContentFilterDetail> {
168    let value = serde_json::from_str::<Value>(body).ok()?;
169    let error = value.get("error").unwrap_or(&value);
170    let inner = error.get("innererror");
171
172    let code = inner
173        .and_then(|i| i.get("code"))
174        .or_else(|| inner.and_then(|i| i.get("type")))
175        .and_then(Value::as_str)
176        // The outer code is the *marker* (`content_filter`), not the policy,
177        // so it is never read as one.
178        .filter(|c| *c != CONTENT_FILTER_MARKER)
179        .map(String::from);
180    let param = error
181        .get("param")
182        .and_then(Value::as_str)
183        .filter(|p| !p.is_empty())
184        .map(String::from);
185
186    let mut categories = std::collections::BTreeMap::new();
187    let results = inner
188        .and_then(|i| i.get("content_filter_result"))
189        .or_else(|| error.get("content_filter_result"))
190        .and_then(Value::as_object);
191    if let Some(results) = results {
192        for (name, verdict) in results {
193            let Some(verdict) = verdict.as_object() else {
194                continue;
195            };
196            categories.insert(
197                name.clone(),
198                ContentFilterCategory {
199                    filtered: verdict
200                        .get("filtered")
201                        .and_then(Value::as_bool)
202                        .unwrap_or(false),
203                    detected: verdict.get("detected").and_then(Value::as_bool),
204                    severity: verdict
205                        .get("severity")
206                        .and_then(Value::as_str)
207                        .map(String::from),
208                },
209            );
210        }
211    }
212
213    if code.is_none() && param.is_none() && categories.is_empty() {
214        return None;
215    }
216    Some(ContentFilterDetail {
217        code,
218        param,
219        categories,
220    })
221}
222
223/// The wire marker that classifies an error as a content-filter refusal, as
224/// opposed to the *policy* code beside it.
225const CONTENT_FILTER_MARKER: &str = "content_filter";
226
227fn is_content_filter_marker(v: &Value) -> bool {
228    v.get("code").and_then(Value::as_str) == Some(CONTENT_FILTER_MARKER)
229        || v.get("type").and_then(Value::as_str) == Some(CONTENT_FILTER_MARKER)
230}
231
232/// An OpenAI (or OpenAI-compatible) chat client.
233#[derive(Clone)]
234pub struct OpenAIChatCompletionClient {
235    inner: Arc<Inner>,
236}
237
238#[derive(Clone)]
239struct Inner {
240    http: reqwest::Client,
241    api_key: String,
242    base_url: String,
243    model: String,
244    organization: Option<String>,
245}
246
247impl OpenAIChatCompletionClient {
248    /// Create a client for the given API key and default model.
249    pub fn new(api_key: impl Into<String>, model: impl Into<String>) -> Self {
250        Self {
251            inner: Arc::new(Inner {
252                http: reqwest::Client::new(),
253                api_key: api_key.into(),
254                base_url: DEFAULT_BASE_URL.to_string(),
255                model: model.into(),
256                organization: None,
257            }),
258        }
259    }
260
261    /// Build a client from the `OPENAI_API_KEY` (and optional
262    /// `OPENAI_BASE_URL`) environment variables.
263    pub fn from_env(model: impl Into<String>) -> Result<Self> {
264        let key = std::env::var("OPENAI_API_KEY")
265            .map_err(|_| Error::Configuration("OPENAI_API_KEY is not set".into()))?;
266        let mut client = Self::new(key, model);
267        if let Ok(base) = std::env::var("OPENAI_BASE_URL") {
268            client = client.with_base_url(base);
269        }
270        Ok(client)
271    }
272
273    /// Override the base URL (for Azure OpenAI or compatible servers).
274    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
275        Arc::make_mut(&mut self.inner).base_url = base_url.into();
276        self
277    }
278
279    /// Set the organization header.
280    pub fn with_organization(mut self, org: impl Into<String>) -> Self {
281        Arc::make_mut(&mut self.inner).organization = Some(org.into());
282        self
283    }
284
285    fn build_body(&self, messages: &[Message], options: &ChatOptions, stream: bool) -> Value {
286        let mut body = Map::new();
287        let model = options
288            .model
289            .clone()
290            .unwrap_or_else(|| self.inner.model.clone());
291        body.insert("model".into(), json!(model));
292        body.insert(
293            "messages".into(),
294            json!(convert::messages_to_openai(messages)),
295        );
296        convert::apply_options(&mut body, options);
297        let (tools, tool_choice) = convert::tools_to_openai(options);
298        if let Some(tools) = tools {
299            body.insert("tools".into(), tools);
300        }
301        if let Some(choice) = tool_choice {
302            body.insert("tool_choice".into(), choice);
303        }
304        if stream {
305            body.insert("stream".into(), json!(true));
306            body.insert("stream_options".into(), json!({ "include_usage": true }));
307        }
308        Value::Object(body)
309    }
310
311    async fn post(&self, body: &Value) -> Result<reqwest::Response> {
312        let url = format!(
313            "{}/chat/completions",
314            self.inner.base_url.trim_end_matches('/')
315        );
316        let mut req = self
317            .inner
318            .http
319            .post(&url)
320            .bearer_auth(&self.inner.api_key)
321            .json(body);
322        if let Some(org) = &self.inner.organization {
323            req = req.header("OpenAI-Organization", org);
324        }
325        let resp = req
326            .send()
327            .await
328            .map_err(|e| Error::service(format!("request failed: {e}")))?;
329        if !resp.status().is_success() {
330            let status = resp.status();
331            let retry_after = parse_retry_after(resp.headers());
332            let text = resp.text().await.unwrap_or_default();
333            return Err(classify_service_error(
334                status.as_u16(),
335                &text,
336                format!("OpenAI API error {status}: {text}"),
337                retry_after,
338            ));
339        }
340        Ok(resp)
341    }
342
343    /// The default model id.
344    pub fn model(&self) -> &str {
345        &self.inner.model
346    }
347}
348
349#[async_trait::async_trait]
350impl ChatClient for OpenAIChatCompletionClient {
351    async fn get_response(
352        &self,
353        messages: Vec<Message>,
354        options: ChatOptions,
355    ) -> Result<ChatResponse> {
356        let body = self.build_body(&messages, &options, false);
357        let resp = self.post(&body).await?;
358        let value: Value = resp
359            .json()
360            .await
361            .map_err(|e| Error::service(format!("invalid response json: {e}")))?;
362        Ok(convert::parse_response(&value))
363    }
364
365    async fn get_streaming_response(
366        &self,
367        messages: Vec<Message>,
368        options: ChatOptions,
369    ) -> Result<ChatStream> {
370        let body = self.build_body(&messages, &options, true);
371        let resp = self.post(&body).await?;
372        Ok(parse_sse_stream(resp).boxed())
373    }
374
375    fn model(&self) -> Option<&str> {
376        Some(&self.inner.model)
377    }
378
379    fn service_url(&self) -> Option<&str> {
380        Some(&self.inner.base_url)
381    }
382}
383
384type ByteStream =
385    std::pin::Pin<Box<dyn futures::Stream<Item = reqwest::Result<bytes::Bytes>> + Send>>;
386
387/// Turn an SSE HTTP response into a stream of [`ChatResponseUpdate`]s.
388///
389/// Public (but hidden) so `agent-framework-azure` can reuse the exact same
390/// chat-completions SSE parsing for Azure OpenAI's wire-compatible stream.
391#[doc(hidden)]
392pub fn parse_sse_stream(
393    resp: reqwest::Response,
394) -> impl futures::Stream<Item = Result<ChatResponseUpdate>> + Send {
395    let byte_stream: ByteStream = Box::pin(resp.bytes_stream());
396    // `tool_ids` maps a streamed tool-call `index` to its `call_id`, so that
397    // continuation chunks (which carry only the index) can be resolved back to
398    // the id assigned in the first chunk.
399    futures::stream::unfold(
400        SseState {
401            byte_stream,
402            buffer: String::new(),
403            utf8: Utf8StreamDecoder::new(),
404            queued: VecDeque::new(),
405            tool_ids: HashMap::new(),
406            logprobs_content: Vec::new(),
407            done: false,
408        },
409        |mut state| async move {
410            loop {
411                if let Some(update) = state.queued.pop_front() {
412                    return Some((Ok(update), state));
413                }
414                if state.done {
415                    return None;
416                }
417                match state.byte_stream.next().await {
418                    Some(Ok(bytes)) => {
419                        let decoded = state.utf8.push(&bytes);
420                        state.buffer.push_str(&decoded);
421                        while let Some(pos) = state.buffer.find('\n') {
422                            let line = state.buffer[..pos].trim().to_string();
423                            state.buffer.drain(..=pos);
424                            if let Some(data) = line.strip_prefix("data:") {
425                                let data = data.trim();
426                                if data == "[DONE]" {
427                                    return drain_or_end(state);
428                                }
429                                if let Ok(value) = serde_json::from_str::<Value>(data) {
430                                    // Providers signal mid-stream failures (rate
431                                    // limits, content filter, billing) as an
432                                    // object with an `error` field; surface it.
433                                    if let Some(err) = value.get("error") {
434                                        let msg = err
435                                            .get("message")
436                                            .and_then(Value::as_str)
437                                            .unwrap_or("unknown stream error")
438                                            .to_string();
439                                        state.done = true;
440                                        return Some((Err(Error::service(msg)), state));
441                                    }
442                                    if let Some(mut update) =
443                                        parse_delta(&value, &mut state.tool_ids)
444                                    {
445                                        accumulate_logprobs(
446                                            &mut update,
447                                            &mut state.logprobs_content,
448                                        );
449                                        state.queued.push_back(update);
450                                    }
451                                }
452                            }
453                        }
454                    }
455                    Some(Err(e)) => {
456                        state.done = true;
457                        return Some((Err(Error::service(format!("stream error: {e}"))), state));
458                    }
459                    None => return drain_or_end(state),
460                }
461            }
462        },
463    )
464}
465
466/// State carried across `unfold` iterations while parsing the SSE stream.
467struct SseState {
468    byte_stream: ByteStream,
469    buffer: String,
470    utf8: Utf8StreamDecoder,
471    queued: VecDeque<ChatResponseUpdate>,
472    tool_ids: HashMap<i64, String>,
473    /// Every token entry seen under `logprobs.content` so far.
474    ///
475    /// Each chunk reports only its own tokens, and aggregation keeps the last
476    /// value it sees for a given `additional_properties` key — so without
477    /// this a streamed-then-aggregated response would carry the final chunk's
478    /// logprobs alone, where the same request made non-streaming returns
479    /// every token's. Two paths of one client answering the same question
480    /// differently, and silently. The running total is attached to the final
481    /// update (see `finish_logprobs`), which is the one aggregation keeps.
482    logprobs_content: Vec<Value>,
483    done: bool,
484}
485
486fn drain_or_end(mut state: SseState) -> Option<(Result<ChatResponseUpdate>, SseState)> {
487    match state.queued.pop_front() {
488        Some(update) => {
489            state.done = true;
490            Some((Ok(update), state))
491        }
492        None => None,
493    }
494}
495
496/// Fold this chunk's token log-probabilities into the running total, and put
497/// the whole total on the final update.
498///
499/// A chunk's `logprobs` describes only that chunk's tokens. Updates are
500/// aggregated by `ChatResponse::absorb_update`, which inserts whatever an
501/// update has under a given `additional_properties` key — so the last chunk
502/// to carry logprobs would be the only one left in the aggregate, while the
503/// same request made non-streaming returns an entry per token. That gap is
504/// invisible to a caller: the key is present either way, just far emptier.
505///
506/// So each update keeps its own chunk's logprobs (a caller reading the stream
507/// sees the deltas as they arrive, unchanged), and the update carrying
508/// `finish_reason` — the one aggregation ends on — carries the accumulated
509/// `content` array instead. Accumulating on *every* update would cost a full
510/// copy per chunk for no gain.
511///
512/// A deliberate divergence from upstream, which keeps only the most recent
513/// chunk's value in its aggregate (#8997 fixed a null chunk *clearing* that
514/// value, not the narrowing). Matching upstream here would mean shipping the
515/// inconsistency between this client's own two paths.
516fn accumulate_logprobs(update: &mut ChatResponseUpdate, running: &mut Vec<Value>) {
517    let key = convert::LOGPROBS_PROPERTY;
518    if let Some(entries) = update
519        .additional_properties
520        .get(key)
521        .and_then(|v| v.get("content"))
522        .and_then(Value::as_array)
523    {
524        running.extend(entries.iter().cloned());
525    }
526    // `finish_reason` marks the end of the choice, so this is the update whose
527    // value survives aggregation. A stream cut off before it leaves the
528    // per-chunk values in place, which is all that can be known.
529    if update.finish_reason.is_some() && !running.is_empty() {
530        update.additional_properties.insert(
531            key.to_string(),
532            serde_json::json!({ "content": std::mem::take(running) }),
533        );
534    }
535}
536
537/// Parse one streamed chunk (`chat.completion.chunk`) into an update, resolving
538/// tool-call ids from the index map.
539fn parse_delta(value: &Value, tool_ids: &mut HashMap<i64, String>) -> Option<ChatResponseUpdate> {
540    let mut update = ChatResponseUpdate {
541        response_id: value.get("id").and_then(Value::as_str).map(String::from),
542        model: value.get("model").and_then(Value::as_str).map(String::from),
543        ..Default::default()
544    };
545
546    let mut contents: Vec<Content> = Vec::new();
547
548    if let Some(choice) = value
549        .get("choices")
550        .and_then(|c| c.as_array())
551        .and_then(|a| a.first())
552    {
553        if let Some(delta) = choice.get("delta") {
554            if let Some(r) = delta.get("role").and_then(Value::as_str) {
555                update.role = Some(Role::new(r));
556            }
557            if let Some(text) = delta.get("content").and_then(Value::as_str) {
558                if !text.is_empty() {
559                    contents.push(Content::Text(TextContent::new(text)));
560                }
561            }
562            // A streamed refusal arrives on its own `delta.refusal` channel,
563            // fragmented like content. Without this it was dropped outright,
564            // so a streamed decline reached the caller as an empty response —
565            // and the non-streaming path's refusal marking had no streaming
566            // counterpart. Marked, so aggregation keeps it separate from
567            // ordinary text and `Message::text` withholds it.
568            if let Some(text) = delta.get("refusal").and_then(Value::as_str) {
569                if !text.is_empty() {
570                    contents.push(Content::Text(TextContent::refusal(text)));
571                }
572            }
573            // Streamed reasoning payload, on the same channel as the
574            // buffered one. See `convert::reasoning_details_of`.
575            if let Some(reasoning) = crate::convert::parse_reasoning_details(delta) {
576                contents.push(reasoning);
577            }
578            if let Some(calls) = delta.get("tool_calls").and_then(Value::as_array) {
579                for call in calls {
580                    let index = call.get("index").and_then(Value::as_i64).unwrap_or(0);
581                    let chunk_id = call.get("id").and_then(Value::as_str).unwrap_or_default();
582                    // The first chunk of a call carries its id; record it so
583                    // later index-only chunks resolve to the same call_id.
584                    let id = if chunk_id.is_empty() {
585                        tool_ids.get(&index).cloned().unwrap_or_default()
586                    } else {
587                        tool_ids.insert(index, chunk_id.to_string());
588                        chunk_id.to_string()
589                    };
590                    let func = call.get("function");
591                    let name = func
592                        .and_then(|f| f.get("name"))
593                        .and_then(Value::as_str)
594                        .unwrap_or_default()
595                        .to_string();
596                    let args = func
597                        .and_then(|f| f.get("arguments"))
598                        .and_then(Value::as_str)
599                        .unwrap_or_default()
600                        .to_string();
601                    contents.push(Content::FunctionCall(FunctionCallContent::new(
602                        id,
603                        name,
604                        Some(FunctionArguments::Raw(args)),
605                    )));
606                }
607            }
608        }
609        if let Some(fr) = choice.get("finish_reason").and_then(Value::as_str) {
610            update.finish_reason = Some(FinishReason::new(fr));
611        }
612        // Only when the chunk actually carries them. A chunk with
613        // `"logprobs": null` — which OpenAI sends freely mid-stream — must not
614        // overwrite the metadata gathered so far, and omitting the key is how
615        // that holds here: `ChatResponse::absorb_update` inserts only the keys
616        // an update has. See `convert::token_logprobs` (upstream #8997).
617        if let Some(logprobs) = convert::token_logprobs(choice) {
618            update
619                .additional_properties
620                .insert(convert::LOGPROBS_PROPERTY.to_string(), logprobs);
621        }
622    }
623
624    // The final chunk (with `stream_options.include_usage`) carries top-level
625    // `usage` and no choices; surface it so streamed runs accumulate token usage
626    // just like non-streaming responses.
627    if let Some(usage) = value.get("usage").filter(|u| u.is_object()) {
628        contents.push(Content::Usage(UsageContent {
629            details: convert::parse_usage(usage),
630        }));
631    }
632
633    if update.role.is_none() {
634        update.role = Some(Role::assistant());
635    }
636    update.contents = contents;
637    Some(update)
638}
639
640#[cfg(test)]
641mod tests {
642    use super::*;
643
644    #[test]
645    fn service_url_reports_the_configured_base_url() {
646        // Read by the observability layer as `server.address` (#9225, #9222).
647        let client = OpenAIChatCompletionClient::new("k", "gpt-4o-mini")
648            .with_base_url("http://gateway.example/v1");
649        assert_eq!(client.service_url(), Some("http://gateway.example/v1"));
650    }
651
652    // -- classify_service_error --------------------------------------------
653    //
654    // Canned status+body combinations run through the exact classification
655    // `OpenAIChatCompletionClient::post` and `responses::OpenAIChatClient::post`
656    // both delegate to.
657
658    #[test]
659    fn a_streamed_reasoning_payload_survives_to_the_aggregated_response() {
660        // The payload arrives on its own delta channel; the aggregated
661        // response is what the next request is built from, so it has to reach
662        // that far to be replayable at all.
663        let mut ids = HashMap::new();
664        let updates: Vec<_> = [
665            serde_json::json!({"choices": [{"delta": {"role": "assistant",
666                "reasoning_details": [{"type": "reasoning.text", "text": "think"}]}}]}),
667            serde_json::json!({"choices": [{"delta": {"content": "answer"}}]}),
668        ]
669        .iter()
670        .filter_map(|v| parse_delta(v, &mut ids))
671        .collect();
672        let resp = agent_framework_core::types::ChatResponse::from_updates(updates);
673        let wire = crate::convert::messages_to_openai(&resp.messages);
674        assert_eq!(
675            wire[0]["reasoning_details"],
676            serde_json::json!([{"type": "reasoning.text", "text": "think"}])
677        );
678    }
679
680    #[test]
681    fn streamed_logprobs_accumulate_across_chunks_and_survive_a_null_chunk() {
682        // Two things at once. Upstream #8997: a chunk with `"logprobs": null`
683        // — which OpenAI sends freely mid-stream, including on the finish
684        // chunk — must not clear what was gathered. And the aggregate must
685        // end up with *every* token's entry, not just the last chunk's:
686        // the same request made non-streaming returns one entry per token,
687        // and a caller cannot see which it got.
688        let first = serde_json::json!({
689            "content": [{ "token": "hel", "logprob": -0.1 }]
690        });
691        let latest = serde_json::json!({
692            "content": [{ "token": "lo", "logprob": -0.2 }]
693        });
694        let mut ids = HashMap::new();
695        let mut running = Vec::new();
696        let updates: Vec<_> = [
697            serde_json::json!({"choices": [{"delta": {"role": "assistant", "content": "hel"},
698                "logprobs": first}]}),
699            serde_json::json!({"choices": [{"delta": {"content": "lo"}, "logprobs": latest}]}),
700            // The finish chunk: text is done and logprobs are explicitly null.
701            serde_json::json!({"choices": [{"delta": {}, "logprobs": null,
702                "finish_reason": "stop"}]}),
703        ]
704        .iter()
705        .filter_map(|v| parse_delta(v, &mut ids))
706        .map(|mut u| {
707            accumulate_logprobs(&mut u, &mut running);
708            u
709        })
710        .collect();
711        assert_eq!(updates.len(), 3);
712
713        // Each non-final update still carries its own chunk, so a caller
714        // reading the stream sees the deltas as they arrive.
715        assert_eq!(
716            updates[0].additional_properties[crate::convert::LOGPROBS_PROPERTY],
717            first
718        );
719        assert_eq!(
720            updates[1].additional_properties[crate::convert::LOGPROBS_PROPERTY],
721            latest
722        );
723
724        // The aggregate has both tokens — the null finish chunk neither
725        // cleared them nor narrowed them to the last.
726        let resp = agent_framework_core::types::ChatResponse::from_updates(updates);
727        let content = resp.additional_properties[crate::convert::LOGPROBS_PROPERTY]["content"]
728            .as_array()
729            .expect("an accumulated content array")
730            .clone();
731        let tokens: Vec<&str> = content
732            .iter()
733            .map(|e| e["token"].as_str().unwrap())
734            .collect();
735        assert_eq!(tokens, vec!["hel", "lo"]);
736        assert_eq!(resp.text(), "hello");
737        assert_eq!(resp.finish_reason.map(|f| f.0).as_deref(), Some("stop"));
738    }
739
740    #[test]
741    fn a_stream_that_never_carries_logprobs_surfaces_no_key() {
742        // Negative control: the common case must not gain a `logprobs: null`
743        // entry it never had, which a guard written at the merge instead of
744        // the read would have produced.
745        let mut ids = HashMap::new();
746        let mut running = Vec::new();
747        let updates: Vec<_> = [
748            serde_json::json!({"choices": [{"delta": {"role": "assistant", "content": "hi"},
749                "logprobs": null}]}),
750            serde_json::json!({"choices": [{"delta": {}, "finish_reason": "stop"}]}),
751        ]
752        .iter()
753        .filter_map(|v| parse_delta(v, &mut ids))
754        .map(|mut u| {
755            accumulate_logprobs(&mut u, &mut running);
756            u
757        })
758        .collect();
759        let resp = agent_framework_core::types::ChatResponse::from_updates(updates);
760        assert!(!resp
761            .additional_properties
762            .contains_key(crate::convert::LOGPROBS_PROPERTY));
763    }
764
765    #[test]
766    fn a_streamed_chat_refusal_is_parsed_and_marked() {
767        // `delta.refusal` is its own channel, fragmented like content. It was
768        // not read at all, so a streamed decline vanished entirely.
769        let mut ids = HashMap::new();
770        let update = parse_delta(
771            &serde_json::json!({
772                "id": "chatcmpl-1",
773                "choices": [{ "delta": { "role": "assistant", "refusal": "I can't" } }],
774            }),
775            &mut ids,
776        )
777        .expect("a refusal delta must produce an update");
778        assert!(
779            matches!(&update.contents[0], Content::Text(t) if t.refusal && t.text == "I can't")
780        );
781
782        // Aggregated across fragments, and kept apart from ordinary text.
783        let mut ids = HashMap::new();
784        let updates: Vec<_> = [
785            serde_json::json!({"choices": [{"delta": {"content": "here: "}}]}),
786            serde_json::json!({"choices": [{"delta": {"refusal": "I can't "}}]}),
787            serde_json::json!({"choices": [{"delta": {"refusal": "help"}}]}),
788        ]
789        .iter()
790        .filter_map(|v| parse_delta(v, &mut ids))
791        .collect();
792        let resp = agent_framework_core::types::ChatResponse::from_updates(updates);
793        assert_eq!(
794            resp.messages[0].refusal_text().as_deref(),
795            Some("I can't help")
796        );
797        // `text()` withholds it rather than presenting the partial answer.
798        assert_eq!(resp.text(), "");
799    }
800
801    #[test]
802    fn classifies_401_and_403_as_invalid_auth() {
803        for status in [401, 403] {
804            let err = classify_service_error(status, "", format!("err {status}"), None);
805            assert!(
806                matches!(err, Error::ServiceInvalidAuth { .. }),
807                "status {status}: {err:?}"
808            );
809        }
810    }
811
812    #[test]
813    fn classifies_400_404_422_as_invalid_request_by_default() {
814        for status in [400, 404, 422] {
815            let body = r#"{"error":{"message":"nope","type":"invalid_request_error"}}"#;
816            let err = classify_service_error(status, body, format!("err {status}"), None);
817            assert!(
818                matches!(err, Error::ServiceInvalidRequest { .. }),
819                "status {status}: {err:?}"
820            );
821        }
822    }
823
824    #[test]
825    fn classifies_400_with_content_filter_code_as_content_filter() {
826        // Plain OpenAI shape: `error.code`.
827        let body = r#"{"error":{"message":"flagged","type":"invalid_request_error","code":"content_filter"}}"#;
828        let err = classify_service_error(400, body, "err", None);
829        assert!(matches!(err, Error::ServiceContentFilter { .. }), "{err:?}");
830    }
831
832    #[test]
833    fn classifies_azure_openai_content_filter_shape_with_innererror() {
834        // Azure OpenAI's shape: outer `error.code` is already "content_filter"
835        // (mirrors `openai/_exceptions.py`'s `OpenAIContentFilterException`,
836        // which is constructed once `ex.code == "content_filter"` is already
837        // known — the nested `innererror.code` carries the more specific
838        // `ResponsibleAIPolicyViolation` detail, not the marker itself), but
839        // this also tolerates the marker living only in `innererror`.
840        let body = r#"{"error":{"message":"The response was filtered","code":"content_filter","innererror":{"code":"ResponsibleAIPolicyViolation"}}}"#;
841        let err = classify_service_error(400, body, "err", None);
842        assert!(matches!(err, Error::ServiceContentFilter { .. }), "{err:?}");
843
844        let nested_only =
845            r#"{"error":{"message":"filtered","innererror":{"code":"content_filter"}}}"#;
846        let err = classify_service_error(400, nested_only, "err", None);
847        assert!(matches!(err, Error::ServiceContentFilter { .. }), "{err:?}");
848    }
849
850    #[test]
851    fn an_azure_content_filter_error_carries_which_category_fired() {
852        // The distinction an application acts on: a self-harm block is not a
853        // jailbreak detection, and a prompt block is not an output block.
854        // With only a message string, none of it is reachable.
855        let body = r#"{"error":{"message":"The response was filtered","code":"content_filter",
856            "param":"prompt","innererror":{"code":"ResponsibleAIPolicyViolation",
857            "content_filter_result":{
858                "hate":{"filtered":false,"severity":"safe"},
859                "self_harm":{"filtered":true,"severity":"medium"},
860                "jailbreak":{"filtered":false,"detected":true}}}}}"#;
861        let err = classify_service_error(400, body, "err", None);
862        let detail = err
863            .content_filter_detail()
864            .expect("an Azure content-filter body carries a breakdown");
865        assert_eq!(
866            detail.code.as_deref(),
867            Some(ContentFilterDetail::RESPONSIBLE_AI_POLICY_VIOLATION)
868        );
869        assert_eq!(detail.param.as_deref(), Some("prompt"));
870        assert_eq!(detail.filtered_categories(), vec!["jailbreak", "self_harm"]);
871        assert_eq!(
872            detail.categories["self_harm"].severity.as_deref(),
873            Some("medium")
874        );
875        assert!(!detail.categories["hate"].filtered);
876    }
877
878    #[test]
879    fn an_unrecognized_policy_code_or_severity_is_carried_rather_than_rejected() {
880        // Upstream parsed both into enums, so a value Azure had not shipped
881        // yet raised *while building the error* — #8393 fixed that for the
882        // code and left it for the severity. An open string has no such mode.
883        let body = r#"{"error":{"code":"content_filter","innererror":{
884            "code":"SomeFutureAzurePolicy",
885            "content_filter_result":{"novel_category":{"filtered":true,"severity":"extreme"}}}}}"#;
886        let err = classify_service_error(400, body, "err", None);
887        let detail = err.content_filter_detail().expect("still classified");
888        assert_eq!(detail.code.as_deref(), Some("SomeFutureAzurePolicy"));
889        assert_eq!(
890            detail.categories["novel_category"].severity.as_deref(),
891            Some("extreme")
892        );
893    }
894
895    #[test]
896    fn a_plain_openai_content_filter_error_grows_no_empty_breakdown() {
897        // Nothing beyond the marker: a detail full of `None` would suggest
898        // the provider reported something it did not.
899        let body = r#"{"error":{"message":"flagged","code":"content_filter"}}"#;
900        let err = classify_service_error(400, body, "err", None);
901        assert!(matches!(err, Error::ServiceContentFilter { .. }), "{err:?}");
902        assert!(err.content_filter_detail().is_none());
903    }
904
905    #[test]
906    fn the_outer_marker_is_never_read_as_the_policy_code() {
907        // `error.code` is "content_filter" on the Azure shape; the policy
908        // lives in `innererror.code`. Reading the marker as the policy would
909        // report every Azure refusal as the same one.
910        let body = r#"{"error":{"code":"content_filter","param":"prompt"}}"#;
911        let err = classify_service_error(400, body, "err", None);
912        let detail = err
913            .content_filter_detail()
914            .expect("param alone is a detail");
915        assert_eq!(detail.code, None);
916        assert_eq!(detail.param.as_deref(), Some("prompt"));
917    }
918
919    #[test]
920    fn non_json_or_unmarked_body_does_not_trigger_content_filter() {
921        let err = classify_service_error(400, "not json", "err", None);
922        assert!(
923            matches!(err, Error::ServiceInvalidRequest { .. }),
924            "{err:?}"
925        );
926
927        let err = classify_service_error(400, r#"{"error":{"code":"other"}}"#, "err", None);
928        assert!(
929            matches!(err, Error::ServiceInvalidRequest { .. }),
930            "{err:?}"
931        );
932    }
933
934    #[test]
935    fn leaves_retryable_statuses_as_service_status() {
936        // 408/429/5xx must stay `ServiceStatus` (carrying status +
937        // retry_after) exactly as before — the retry layer depends on it.
938        for status in [408, 429, 500, 503] {
939            let err = classify_service_error(status, "", format!("err {status}"), Some(2.0));
940            assert_eq!(err.status(), Some(status), "{err:?}");
941            assert_eq!(err.retry_after(), Some(2.0), "{err:?}");
942        }
943    }
944
945    #[test]
946    fn unclassified_4xx_stays_service_status() {
947        // e.g. 409 Conflict, 413 Payload Too Large: not in the
948        // auth/invalid-request/content-filter buckets, so this falls back to
949        // the generic status-carrying variant rather than guessing.
950        let err = classify_service_error(409, "", "err", None);
951        assert_eq!(err.status(), Some(409), "{err:?}");
952    }
953
954    #[test]
955    fn message_text_is_preserved_verbatim() {
956        let err = classify_service_error(401, "", "OpenAI API error 401: unauthorized", None);
957        assert_eq!(
958            err.to_string(),
959            "service error: OpenAI API error 401: unauthorized"
960        );
961    }
962}