rai-sdk 0.2.0

Rust AI SDK — unified client for OpenAI, Anthropic, and OpenRouter with typed models, structured output, tool calling, and streaming
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! Provider for endpoints that speak the OpenAI Chat Completions wire format.
//!
//! Ollama, vLLM, LM Studio, llama.cpp's server, LocalAI, Text Generation
//! Inference and most inference gateways all expose
//! `POST {base_url}/chat/completions` with OpenAI's request and response
//! shapes. This provider targets that format rather than any one service:
//! the endpoint is named per client, so a process can hold several clients
//! pointed at different servers at once.
//!
//! ```no_run
//! use rai_sdk::{ClientBuilder, Model};
//!
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let client = ClientBuilder::new()
//!     .ollama()
//!     .model(Model::openai_compatible("llama3.1:8b"))
//!     .build()?;
//!
//! let response = client.request().prompt("Hello").generate().await?;
//! # println!("{}", response.text());
//! # Ok(())
//! # }
//! ```
//!
//! # How it differs from the OpenAI provider
//!
//! The request builder and the SSE parser are shared with
//! [`openai`](super::openai) — the wire format is the same, and forking it
//! would mean two parsers drifting apart. What differs is everything around it:
//!
//! - **Authentication is optional.** Local runtimes usually accept no
//!   credential. When no key is configured no `Authorization` header is sent at
//!   all, rather than a placeholder key being invented.
//! - **The endpoint is required.** There is no default base URL and no
//!   environment variable; a client without one simply has no compatible
//!   provider.
//! - **Model IDs are free-form.** [`OpenAICompatibleModel`] wraps whatever the
//!   server calls the model, with no catalog to match against.
//! - **`strict` JSON schemas are not requested.** OpenAI's strict mode is a
//!   hard guarantee worth asking for; third-party endpoints implement it
//!   unevenly, some rejecting the flag outright. Structured output is validated
//!   against the schema client-side either way, so the flag is sent as `false`.
//! - **Capability gaps are typed.** See below.
//!
//! The shared stream parser already tolerates the framing divergences that show
//! up across self-hosted servers: `data:` with no space after the colon (the
//! SSE specification makes it optional), a missing `[DONE]` sentinel, absent
//! usage when `stream_options.include_usage` is ignored, and tool-call
//! continuation deltas that omit the call id. Payloads it cannot parse are
//! logged and skipped, so one unrecognized frame cannot truncate a response.
//!
//! # Capability degradation
//!
//! "OpenAI-compatible" describes a wire format, not a feature set. A 3B model
//! behind Ollama may not call tools; a runtime may not constrain output to a
//! JSON Schema. Both cases surface as
//! [`Error::CapabilityUnsupported`] — a
//! variant distinct from the generic HTTP and request errors — so a caller can
//! branch on [`Error::unsupported_capability`](crate::Error::unsupported_capability)
//! and fall back instead of parsing an error string.
//!
//! That error arrives by either of two routes:
//!
//! 1. **Declared.** [`EndpointCapabilities`] says the endpoint lacks the
//!    capability, and the request is refused locally before any HTTP call.
//! 2. **Observed.** The endpoint itself rejects a request that used the
//!    capability, and its own message is classified. Classification only
//!    applies when the request actually carried tools or a `response_format`,
//!    so an unrelated bad request stays an ordinary
//!    [`Error::InvalidRequest`].
//!
//! Nothing is probed: capabilities are declared by the caller, never
//! auto-detected.

use std::pin::Pin;

use futures::Stream;
use reqwest::{Client, RequestBuilder};
use serde::Deserialize;
use tracing::{debug, error, info, instrument};

use super::openai::{ChatRequestOptions, build_chat_request, parse_chat_completions_sse};
use crate::{
    config::{Config, EndpointCapabilities},
    error::{Capability, Error, ProviderKind, Result},
    message::{Message, Prompt, Response, ToolCall, ToolDefinition, Usage},
    model::OpenAICompatibleModel,
};

/// Provider for a single OpenAI-compatible endpoint.
///
/// Stores the endpoint, its optional bearer token, the capabilities it was
/// declared to have, and the HTTP client, all fixed at construction time. One
/// instance serves one endpoint; point a second [`Client`](crate::Client) at a
/// second URL to use both.
pub struct OpenAICompatibleProvider {
    client: Client,
    base_url: String,
    api_key: Option<String>,
    capabilities: EndpointCapabilities,
}

impl std::fmt::Debug for OpenAICompatibleProvider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OpenAICompatibleProvider")
            .field("base_url", &self.base_url)
            .field(
                "api_key",
                match &self.api_key {
                    Some(_) => &"[REDACTED]",
                    None => &"[UNSET]",
                },
            )
            .field("capabilities", &self.capabilities)
            .finish()
    }
}

impl OpenAICompatibleProvider {
    /// Create a provider for the endpoint named in `config`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ProviderNotConfigured`] if no base URL is configured —
    /// unlike the other providers, a missing API key is not an error — or
    /// [`Error::Config`] if the HTTP client cannot be built.
    pub fn new(config: &Config) -> Result<Self> {
        let base_url = config
            .openai_compatible_base_url()
            .ok_or(Error::ProviderNotConfigured(ProviderKind::OpenAICompatible))?;

        let client = super::http_client_builder()
            .timeout(std::time::Duration::from_secs(config.timeout()))
            .build()
            .map_err(|e| Error::Config(format!("Failed to create HTTP client: {e}")))?;

        Ok(Self {
            client,
            base_url,
            api_key: config.openai_compatible_key(),
            capabilities: config.openai_compatible_capabilities(),
        })
    }

    /// The endpoint this provider talks to.
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// What this endpoint was declared to support.
    pub fn capabilities(&self) -> EndpointCapabilities {
        self.capabilities
    }

    /// Generate a completion (non-streaming).
    ///
    /// # Errors
    ///
    /// Returns [`Error::CapabilityUnsupported`] when the request needs tool
    /// calling or structured output and the endpoint cannot provide it,
    /// [`Error::Http`] if the request cannot be sent,
    /// [`Error::Auth`]/[`Error::RateLimit`]/[`Error::InvalidRequest`]/[`Error::Request`]
    /// depending on the status code the endpoint replies with, and
    /// [`Error::Request`] if the response contains no choices or a tool call
    /// with unparseable arguments.
    #[instrument(skip(self, prompt, config))]
    pub async fn generate(
        &self,
        model: &OpenAICompatibleModel,
        prompt: &Prompt,
        config: &crate::generation::GenerationConfig,
        tool_definitions: Option<&[ToolDefinition]>,
    ) -> Result<Response> {
        info!(
            model = model.as_str(),
            base_url = %self.base_url,
            "Generating completion with an OpenAI-compatible endpoint"
        );

        let requested = RequestedCapabilities::of(config, tool_definitions);
        self.check_declared_capabilities(requested)?;

        let request = self.build_request(model, prompt, config, false, tool_definitions);
        let url = self.chat_completions_url();

        debug!(url = %url, "Sending request to the OpenAI-compatible endpoint");
        debug!(request_payload = ?request, "OpenAI-compatible request payload");

        let response = self.authorized(&url).json(&request).send().await?;

        let status = response.status();
        if !status.is_success() {
            let error_body = response.text().await.unwrap_or_default();
            error!(status = %status, body = %error_body, "OpenAI-compatible request failed");
            return Err(self.parse_error(status.as_u16(), &error_body, requested));
        }

        let response_body: ChatCompletionsResponse = response.json().await?;
        debug!(response_payload = ?response_body, "OpenAI-compatible response payload");
        self.parse_response(model, response_body)
    }

    /// Generate a completion with streaming.
    ///
    /// The returned stream yields
    /// [`ProviderStreamEvent`](crate::provider::ProviderStreamEvent) values and
    /// ends on the `[DONE]` sentinel or when the endpoint closes the body,
    /// whichever comes first — self-hosted servers do not all send the
    /// sentinel.
    ///
    /// # Errors
    ///
    /// Returns the same errors as [`OpenAICompatibleProvider::generate`] while
    /// opening the stream. Transport failures encountered mid-stream are
    /// yielded as [`Error::Stream`] items.
    #[instrument(skip(self, prompt, config))]
    pub async fn generate_stream(
        &self,
        model: &OpenAICompatibleModel,
        prompt: &Prompt,
        config: &crate::generation::GenerationConfig,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<crate::provider::ProviderStreamEvent>> + Send>>>
    {
        // Streaming never advertises tools — the crate rejects tool-bearing
        // streaming requests earlier — but `response_format` still rides along.
        let requested = RequestedCapabilities::of(config, None);
        self.check_declared_capabilities(requested)?;

        let request = self.build_request(model, prompt, config, true, None);
        let url = self.chat_completions_url();

        debug!(url = %url, "Sending streaming request to the OpenAI-compatible endpoint");

        let response = self.authorized(&url).json(&request).send().await?;

        let status = response.status();
        if !status.is_success() {
            let error_body = response.text().await.unwrap_or_default();
            error!(status = %status, body = %error_body, "OpenAI-compatible streaming request failed");
            return Err(self.parse_error(status.as_u16(), &error_body, requested));
        }

        let byte_stream = response.bytes_stream();

        Ok(Box::pin(parse_chat_completions_sse(byte_stream)))
    }

    fn chat_completions_url(&self) -> String {
        format!("{}/chat/completions", self.base_url)
    }

    /// Start a POST, adding `Authorization` only when a key was configured.
    ///
    /// Local endpoints typically reject nothing and require nothing; sending a
    /// made-up bearer token would be indistinguishable from a real credential
    /// leaking into logs on the far side.
    fn authorized(&self, url: &str) -> RequestBuilder {
        let request = self
            .client
            .post(url)
            .header("Content-Type", "application/json");

        match &self.api_key {
            Some(key) => request.header("Authorization", format!("Bearer {key}")),
            None => request,
        }
    }

    fn build_request(
        &self,
        model: &OpenAICompatibleModel,
        prompt: &Prompt,
        config: &crate::generation::GenerationConfig,
        stream: bool,
        tool_definitions: Option<&[ToolDefinition]>,
    ) -> super::openai::OpenAIRequest {
        build_chat_request(
            prompt,
            config,
            ChatRequestOptions {
                model: model.as_str(),
                stream,
                tool_definitions,
                // There is no reasoning-model catalog to consult here, and no
                // compatible endpoint is known to reject sampling parameters.
                drop_sampling_params: false,
                strict_json_schema: false,
            },
        )
    }

    /// Refuse a request the endpoint was declared unable to serve.
    fn check_declared_capabilities(&self, requested: RequestedCapabilities) -> Result<()> {
        if requested.tool_calling && !self.capabilities.tool_calling {
            return Err(self.capability_unsupported(
                Capability::ToolCalling,
                "the endpoint was configured as not supporting tool calling",
            ));
        }

        if requested.structured_output && !self.capabilities.structured_output {
            return Err(self.capability_unsupported(
                Capability::StructuredOutput,
                "the endpoint was configured as not supporting structured output",
            ));
        }

        Ok(())
    }

    fn capability_unsupported(&self, capability: Capability, message: &str) -> Error {
        Error::CapabilityUnsupported {
            provider: ProviderKind::OpenAICompatible,
            capability,
            base_url: self.base_url.clone(),
            message: message.to_string(),
        }
    }

    fn parse_response(
        &self,
        model: &OpenAICompatibleModel,
        response: ChatCompletionsResponse,
    ) -> Result<Response> {
        let choice = response.choices.first().ok_or_else(|| Error::Request {
            provider: ProviderKind::OpenAICompatible,
            message: "No choices in response".to_string(),
        })?;

        let content = choice.message.content.clone().unwrap_or_default();
        let tool_calls = choice
            .message
            .tool_calls
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|tc| {
                let arguments = serde_json::from_str(&tc.function.arguments).map_err(|error| {
                    Error::Request {
                        provider: ProviderKind::OpenAICompatible,
                        message: format!(
                            "Invalid tool arguments returned for '{}': {error}",
                            tc.function.name
                        ),
                    }
                })?;

                Ok(ToolCall {
                    id: tc.id,
                    name: tc.function.name,
                    arguments,
                })
            })
            .collect::<Result<Vec<_>>>()?;

        let usage = response.usage.map(|u| Usage {
            prompt_tokens: Some(u.prompt_tokens),
            completion_tokens: Some(u.completion_tokens),
            total_tokens: Some(u.total_tokens),
        });

        info!(
            model = model.as_str(),
            finish_reason = ?choice.finish_reason,
            "Received response from the OpenAI-compatible endpoint"
        );

        Ok(Response {
            messages: vec![Message::assistant_with_tool_calls(content, tool_calls)],
            usage,
            model: model.as_str().to_string(),
            provider: ProviderKind::OpenAICompatible,
            finish_reason: choice.finish_reason.clone(),
        })
    }

    /// Map an unsuccessful HTTP response onto the crate's error type.
    ///
    /// Before the usual status-code mapping, a rejection that names something
    /// the request actually asked for — tools, or a `response_format` — is
    /// reported as [`Error::CapabilityUnsupported`] rather than as a generic
    /// bad request, since "this endpoint cannot do that" and "you sent
    /// nonsense" call for different handling.
    fn parse_error(&self, status: u16, body: &str, requested: RequestedCapabilities) -> Error {
        let message = super::openai::parse_error_message(body);

        if let Some(message) = &message {
            if CAPABILITY_REJECTION_STATUSES.contains(&status) {
                if requested.tool_calling && mentions_unsupported(message, TOOL_SUBJECTS) {
                    return self.capability_unsupported(Capability::ToolCalling, message);
                }
                if requested.structured_output
                    && mentions_unsupported(message, STRUCTURED_OUTPUT_SUBJECTS)
                {
                    return self.capability_unsupported(Capability::StructuredOutput, message);
                }
            }
        }

        let Some(message) = message else {
            return Error::Request {
                provider: ProviderKind::OpenAICompatible,
                message: format!("HTTP {status}: {body}"),
            };
        };

        match status {
            401 | 403 => Error::Auth {
                provider: ProviderKind::OpenAICompatible,
                message,
            },
            429 => Error::RateLimit {
                provider: ProviderKind::OpenAICompatible,
                message,
            },
            400 => Error::InvalidRequest(message),
            _ => Error::Request {
                provider: ProviderKind::OpenAICompatible,
                message,
            },
        }
    }
}

/// Which optional capabilities a single request depends on.
///
/// Capability errors are only raised for what the request actually used, so a
/// text-only call against a text-only endpoint never trips them.
#[derive(Debug, Clone, Copy)]
struct RequestedCapabilities {
    tool_calling: bool,
    structured_output: bool,
}

impl RequestedCapabilities {
    fn of(
        config: &crate::generation::GenerationConfig,
        tool_definitions: Option<&[ToolDefinition]>,
    ) -> Self {
        Self {
            tool_calling: tool_definitions.is_some_and(|tools| !tools.is_empty()),
            structured_output: config.json_schema.is_some() || config.json_mode == Some(true),
        }
    }
}

/// Statuses a server plausibly uses to say "I do not implement that".
///
/// Deliberately excludes 5xx other than 501: a crashing endpoint is not an
/// endpoint that lacks a feature, and misreporting one as the other would send
/// callers down a permanent fallback path over a transient fault.
const CAPABILITY_REJECTION_STATUSES: &[u16] = &[400, 404, 422, 501];

const TOOL_SUBJECTS: &[&str] = &["tool", "function call", "function_call"];

const STRUCTURED_OUTPUT_SUBJECTS: &[&str] = &[
    "response_format",
    "response format",
    "json_schema",
    "json schema",
    "structured output",
    "guided decoding",
    "grammar",
];

/// Whether `message` says one of `subjects` is unavailable.
///
/// Both halves must match: naming the subject alone would classify "tool
/// arguments were invalid" as a missing capability.
fn mentions_unsupported(message: &str, subjects: &[&str]) -> bool {
    const UNAVAILABLE: &[&str] = &[
        "not support",
        "unsupported",
        "not implemented",
        "unrecognized",
        "unknown",
        "no support",
        "does not accept",
    ];

    let message = message.to_ascii_lowercase();

    subjects.iter().any(|subject| message.contains(subject))
        && UNAVAILABLE.iter().any(|marker| message.contains(marker))
}

// ── Response types ─────────────────────────────────────────────────────────
//
// Structurally identical to OpenAI's, which is the point of the format. They
// are named separately here so this provider's parsing does not silently
// inherit an OpenAI-specific change to those types.

#[derive(Debug, Deserialize)]
struct ChatCompletionsResponse {
    choices: Vec<ChatCompletionsChoice>,
    usage: Option<ChatCompletionsUsage>,
}

#[derive(Debug, Deserialize)]
struct ChatCompletionsChoice {
    message: ChatCompletionsMessage,
    finish_reason: Option<String>,
}

#[derive(Debug, Deserialize)]
struct ChatCompletionsMessage {
    content: Option<String>,
    tool_calls: Option<Vec<ChatCompletionsToolCall>>,
}

#[derive(Debug, Clone, Deserialize)]
struct ChatCompletionsToolCall {
    id: String,
    function: ChatCompletionsFunctionCall,
}

#[derive(Debug, Clone, Deserialize)]
struct ChatCompletionsFunctionCall {
    name: String,
    arguments: String,
}

#[derive(Debug, Deserialize)]
struct ChatCompletionsUsage {
    prompt_tokens: i32,
    completion_tokens: i32,
    total_tokens: i32,
}

// ── Tests ──────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generation::GenerationConfig;

    fn test_provider(capabilities: EndpointCapabilities) -> OpenAICompatibleProvider {
        OpenAICompatibleProvider {
            client: Client::new(),
            base_url: "http://localhost:11434/v1".to_string(),
            api_key: None,
            capabilities,
        }
    }

    fn tool() -> ToolDefinition {
        ToolDefinition {
            name: "get_weather".to_string(),
            description: None,
            input_schema: serde_json::json!({ "type": "object" }),
        }
    }

    #[test]
    fn requested_capabilities_track_what_the_request_uses() {
        let plain = RequestedCapabilities::of(&GenerationConfig::new(), None);
        assert!(!plain.tool_calling);
        assert!(!plain.structured_output);

        let empty_tools = RequestedCapabilities::of(&GenerationConfig::new(), Some(&[]));
        assert!(!empty_tools.tool_calling);

        let with_tools = RequestedCapabilities::of(&GenerationConfig::new(), Some(&[tool()]));
        assert!(with_tools.tool_calling);

        let json_mode =
            RequestedCapabilities::of(&GenerationConfig::new().with_json_mode(true), None);
        assert!(json_mode.structured_output);

        let schema = RequestedCapabilities::of(
            &GenerationConfig::new().with_json_schema(serde_json::json!({ "type": "object" })),
            None,
        );
        assert!(schema.structured_output);
    }

    #[test]
    fn declared_gaps_are_refused_before_any_request() {
        let provider = test_provider(EndpointCapabilities::text_only());

        let error = provider
            .check_declared_capabilities(RequestedCapabilities::of(
                &GenerationConfig::new(),
                Some(&[tool()]),
            ))
            .expect_err("tool calling should be refused");
        assert_eq!(
            error.unsupported_capability(),
            Some(Capability::ToolCalling)
        );

        let error = provider
            .check_declared_capabilities(RequestedCapabilities::of(
                &GenerationConfig::new().with_json_mode(true),
                None,
            ))
            .expect_err("structured output should be refused");
        assert_eq!(
            error.unsupported_capability(),
            Some(Capability::StructuredOutput)
        );
    }

    #[test]
    fn a_fully_capable_endpoint_refuses_nothing() {
        let provider = test_provider(EndpointCapabilities::default());

        provider
            .check_declared_capabilities(RequestedCapabilities::of(
                &GenerationConfig::new().with_json_mode(true),
                Some(&[tool()]),
            ))
            .expect("a fully capable endpoint should accept everything");
    }

    #[test]
    fn unsupported_markers_need_both_a_subject_and_a_denial() {
        assert!(mentions_unsupported(
            "registry/llama3.2:1b does not support tools",
            TOOL_SUBJECTS
        ));
        assert!(mentions_unsupported(
            "unsupported parameter: response_format",
            STRUCTURED_OUTPUT_SUBJECTS
        ));

        // Naming the subject is not enough on its own.
        assert!(!mentions_unsupported(
            "invalid arguments for tool 'get_weather'",
            TOOL_SUBJECTS
        ));
        // Neither is a denial about something else.
        assert!(!mentions_unsupported(
            "unknown model 'llama3.1:8b'",
            TOOL_SUBJECTS
        ));
    }

    #[test]
    fn capability_classification_only_applies_to_what_was_sent() {
        let provider = test_provider(EndpointCapabilities::default());
        let body = serde_json::json!({
            "error": { "message": "this model does not support tools" }
        })
        .to_string();

        let with_tools = provider.parse_error(
            400,
            &body,
            RequestedCapabilities {
                tool_calling: true,
                structured_output: false,
            },
        );
        assert_eq!(
            with_tools.unsupported_capability(),
            Some(Capability::ToolCalling)
        );

        let without_tools = provider.parse_error(
            400,
            &body,
            RequestedCapabilities {
                tool_calling: false,
                structured_output: false,
            },
        );
        assert!(without_tools.unsupported_capability().is_none());
        assert!(matches!(without_tools, Error::InvalidRequest(_)));
    }

    #[test]
    fn transport_failures_are_not_mistaken_for_capability_gaps() {
        let provider = test_provider(EndpointCapabilities::default());
        let body = serde_json::json!({
            "error": { "message": "tools are not supported right now" }
        })
        .to_string();

        let error = provider.parse_error(
            503,
            &body,
            RequestedCapabilities {
                tool_calling: true,
                structured_output: false,
            },
        );

        assert!(error.unsupported_capability().is_none());
        assert!(matches!(error, Error::Request { .. }));
    }

    #[test]
    fn non_json_error_bodies_keep_their_status_and_text() {
        let provider = test_provider(EndpointCapabilities::default());

        let error = provider.parse_error(
            502,
            "<html>bad gateway</html>",
            RequestedCapabilities {
                tool_calling: false,
                structured_output: false,
            },
        );

        assert!(error.to_string().contains("HTTP 502"));
    }

    #[test]
    fn structured_output_does_not_request_strict_schemas() {
        let provider = test_provider(EndpointCapabilities::default());
        let prompt = Prompt::single(Message::user("hello"));
        let config =
            GenerationConfig::new().with_json_schema(serde_json::json!({ "type": "object" }));

        let request = provider.build_request(
            &OpenAICompatibleModel::new("llama3.1:8b"),
            &prompt,
            &config,
            false,
            None,
        );

        let body = serde_json::to_value(&request).expect("request should serialize");
        assert_eq!(body["response_format"]["json_schema"]["strict"], false);
        assert_eq!(body["model"], "llama3.1:8b");
    }
}