kowalski-core 2.3.1

Core library of Kowalski, a Rust multi-agent framework: agents with MCP tools and memory, and hordes (step pipelines with triggers) over Ollama or OpenAI-compatible models
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
use super::provider::{ChatOutcome, LLMProvider, TokenStream, ToolDefinition, schema_wire_name};
use crate::conversation::{FunctionCall, Message, ToolCall};
use crate::error::KowalskiError;
use crate::rookery::ensure_schema_supported;
use async_openai::{
    Client,
    config::OpenAIConfig,
    types::{
        chat::{
            ChatCompletionMessageToolCall, ChatCompletionMessageToolCalls,
            ChatCompletionRequestAssistantMessageArgs, ChatCompletionRequestMessage,
            ChatCompletionRequestSystemMessageArgs, ChatCompletionRequestToolMessageArgs,
            ChatCompletionRequestUserMessageArgs, ChatCompletionTool, ChatCompletionTools,
            CreateChatCompletionRequest, CreateChatCompletionRequestArgs,
            CreateChatCompletionResponse, FunctionCall as OpenAIFunctionCall, FunctionObject,
            ResponseFormat, ResponseFormatJsonSchema,
        },
        embeddings::CreateEmbeddingRequestArgs,
    },
};
use async_trait::async_trait;
use futures::StreamExt;

pub struct OpenAIProvider {
    client: Client<OpenAIConfig>,
    embedding_model: String,
    /// Effective HTTP API root (for operator-facing errors).
    api_base_display: String,
    native_tools: bool,
    structured_output: bool,
}

impl OpenAIProvider {
    /// `api_key` may be empty for some local OpenAI-compatible servers.
    /// `api_base` should be the full OpenAI API root (e.g. `https://api.openai.com/v1` or `http://localhost:1234/v1`).
    pub fn new(api_key: &str, api_base: Option<&str>) -> Self {
        let mut config = OpenAIConfig::new().with_api_key(api_key);
        let api_base_display = if let Some(base) = api_base {
            let trimmed = base.trim();
            if !trimmed.is_empty() {
                config = config.with_api_base(trimmed);
                trimmed.to_string()
            } else {
                "https://api.openai.com/v1".to_string()
            }
        } else {
            "https://api.openai.com/v1".to_string()
        };
        let client = Client::with_config(config);
        Self {
            client,
            embedding_model: "text-embedding-3-small".to_string(),
            api_base_display,
            native_tools: false,
            structured_output: false,
        }
    }

    /// Opt in to native tool calling (`[llm] native_tools`); requires a tool-capable model.
    pub fn with_native_tools(mut self, enabled: bool) -> Self {
        self.native_tools = enabled;
        self
    }

    /// Opt in to constrained structured output (`[llm] structured_output`); the server must
    /// support strict `response_format: json_schema`.
    pub fn with_structured_output(mut self, enabled: bool) -> Self {
        self.structured_output = enabled;
        self
    }

    fn troubleshoot_chat(&self, model: &str, err: impl std::fmt::Display) -> String {
        format!(
            "OpenAI-compatible chat failed (model `{}`, API base `{}`): {}.\n\
             What to check:\n\
             - `config.toml` `[llm]` `provider = \"openai\"` and `openai_api_base` if you use a non-default host (must usually end with `/v1` for OpenAI-compatible HTTP APIs).\n\
             - **API key**: required for `api.openai.com`; many local servers accept an empty or placeholder key.\n\
             - **Model id**: must match the provider (e.g. `gpt-4o-mini`) or your local server’s model list.\n\
             - **Network**: VPN, firewall, corporate proxy, or TLS MITM breaking HTTPS.\n\
             - **Provider logs**: inspect the OpenAI-compatible server console for 4xx/5xx details.",
            model, self.api_base_display, err
        )
    }

    fn troubleshoot_embed(&self, err: impl std::fmt::Display) -> String {
        format!(
            "OpenAI-compatible embeddings failed (model `{}`, API base `{}`): {}.\n\
             What to check:\n\
             - Same connectivity and API key rules as chat.\n\
             - Embedding model id is valid for that provider (default here: `{}`).\n\
             - Local gateways: some require an explicit embeddings route or a different model name.",
            self.embedding_model, self.api_base_display, err, self.embedding_model
        )
    }
}

#[async_trait]
impl LLMProvider for OpenAIProvider {
    async fn chat(&self, model: &str, messages: &[Message]) -> Result<String, KowalskiError> {
        let openai_messages = messages_to_openai(messages)?;

        let request = CreateChatCompletionRequestArgs::default()
            .model(model)
            .messages(openai_messages)
            .build()
            .map_err(|e| KowalskiError::Initialization(format!("OpenAI request error: {}", e)))?;

        let response = self
            .client
            .chat()
            .create(request)
            .await
            .map_err(|e| KowalskiError::Server(self.troubleshoot_chat(model, &e)))?;

        let n_choices = response.choices.len();
        let content = response
            .choices
            .first()
            .and_then(|choice| choice.message.content.clone())
            .ok_or_else(|| {
                let finish = response
                    .choices
                    .first()
                    .and_then(|c| c.finish_reason)
                    .map(|r| format!(" first_choice_finish_reason={:?}", r))
                    .unwrap_or_default();
                KowalskiError::Server(format!(
                    "No assistant text in OpenAI-compatible chat response (model `{}`, API base `{}`, {} choice(s){}).\n\
                     What to check: moderation or safety filters, `max_tokens` / empty completion, wrong model id, or a local server returning an unexpected schema.",
                    model, self.api_base_display, n_choices, finish
                ))
            })?;

        Ok(content)
    }

    async fn embed(&self, text: &str) -> Result<Vec<f32>, KowalskiError> {
        let request = CreateEmbeddingRequestArgs::default()
            .model(&self.embedding_model)
            .input(text)
            .build()
            .map_err(|e| KowalskiError::Initialization(format!("OpenAI embedding error: {}", e)))?;

        let response = self
            .client
            .embeddings()
            .create(request)
            .await
            .map_err(|e| KowalskiError::Memory(self.troubleshoot_embed(&e)))?;

        let n = response.data.len();
        let embedding = response
            .data
            .first()
            .map(|data| data.embedding.clone())
            .ok_or_else(|| {
                KowalskiError::Memory(format!(
                    "No embedding row in OpenAI-compatible response (embedding model `{}`, API base `{}`, {} row(s)).\n\
                     What to check: model supports embeddings on this provider, quota/rate limits, and response schema.",
                    self.embedding_model, self.api_base_display, n
                ))
            })?;

        Ok(embedding)
    }

    fn supports_streaming(&self) -> bool {
        true
    }

    fn supports_native_tools(&self, _model: &str) -> bool {
        self.native_tools
    }

    fn supports_structured_output(&self, _model: &str) -> bool {
        self.structured_output
    }

    async fn chat_with_schema(
        &self,
        model: &str,
        messages: &[Message],
        schema: &serde_json::Value,
    ) -> Result<String, KowalskiError> {
        let request = build_schema_request(model, messages, schema)?;

        let response = self
            .client
            .chat()
            .create(request)
            .await
            .map_err(|e| KowalskiError::Server(self.troubleshoot_chat(model, &e)))?;

        let n_choices = response.choices.len();
        response
            .choices
            .first()
            .and_then(|choice| choice.message.content.clone())
            .ok_or_else(|| {
                let finish = response
                    .choices
                    .first()
                    .and_then(|c| c.finish_reason)
                    .map(|r| format!(" first_choice_finish_reason={:?}", r))
                    .unwrap_or_default();
                KowalskiError::Server(format!(
                    "No assistant text in OpenAI-compatible structured-output response (model `{}`, API base `{}`, {} choice(s){}).\n\
                     What to check: the server supports `response_format: json_schema` (strict structured outputs), `max_tokens` / empty completion, or a refusal instead of content.",
                    model, self.api_base_display, n_choices, finish
                ))
            })
    }

    async fn chat_with_tool_defs(
        &self,
        model: &str,
        messages: &[Message],
        tools: &[ToolDefinition],
    ) -> Result<ChatOutcome, KowalskiError> {
        let openai_messages = messages_to_openai(messages)?;

        let request = CreateChatCompletionRequestArgs::default()
            .model(model)
            .messages(openai_messages)
            .tools(tool_defs_to_openai(tools))
            .build()
            .map_err(|e| KowalskiError::Initialization(format!("OpenAI request error: {}", e)))?;

        let response = self
            .client
            .chat()
            .create(request)
            .await
            .map_err(|e| KowalskiError::Server(self.troubleshoot_chat(model, &e)))?;

        outcome_from_response(&response).ok_or_else(|| {
            let finish = response
                .choices
                .first()
                .and_then(|c| c.finish_reason)
                .map(|r| format!(" first_choice_finish_reason={:?}", r))
                .unwrap_or_default();
            KowalskiError::Server(format!(
                "No assistant text or tool calls in OpenAI-compatible chat response (model `{}`, API base `{}`, {} choice(s){}).\n\
                 What to check: moderation or safety filters, `max_tokens` / empty completion, wrong model id, or a local server returning an unexpected schema.",
                model, self.api_base_display, response.choices.len(), finish
            ))
        })
    }

    fn chat_stream(&self, model: &str, messages: Vec<Message>) -> TokenStream<'_> {
        let openai_messages = match messages_to_openai(&messages) {
            Ok(m) => m,
            Err(e) => {
                return Box::pin(futures::stream::once(async move { Err(e) }));
            }
        };
        let request = match CreateChatCompletionRequestArgs::default()
            .model(model.to_string())
            .messages(openai_messages)
            .stream(true)
            .build()
        {
            Ok(r) => r,
            Err(e) => {
                return Box::pin(futures::stream::once(async move {
                    Err(KowalskiError::Initialization(format!(
                        "OpenAI stream request: {e}"
                    )))
                }));
            }
        };
        let client = self.client.clone();
        let base = self.api_base_display.clone();
        let model_s = model.to_string();
        Box::pin(async_stream::stream! {
            let mut stream = match client.chat().create_stream(request).await {
                Ok(s) => s,
                Err(e) => {
                    yield Err(KowalskiError::Server(format!(
                        "OpenAI-compatible chat stream failed to start (model `{}`, API base `{}`): {}.\n\
                         What to check: same as non-stream chat — API base, key, model id, and that the server supports streaming for this model.",
                        model_s, base, e
                    )));
                    return;
                }
            };
            while let Some(item) = stream.next().await {
                match item {
                    Ok(resp) => {
                        for choice in resp.choices {
                            if let Some(ref c) = choice.delta.content
                                && !c.is_empty() {
                                    yield Ok(c.clone());
                                }
                        }
                    }
                    Err(e) => {
                        yield Err(KowalskiError::Server(format!(
                            "OpenAI-compatible chat stream chunk error (model `{}`, API base `{}`): {}.\n\
                             What to check: provider timeout, connection drop, or mid-stream API error; retry and inspect server logs.",
                            model_s, base, e
                        )));
                        return;
                    }
                }
            }
        })
    }
}

fn messages_to_openai(
    messages: &[Message],
) -> Result<Vec<ChatCompletionRequestMessage>, KowalskiError> {
    let mut openai_messages: Vec<ChatCompletionRequestMessage> = Vec::new();

    for msg in messages {
        match msg.role.as_str() {
            "system" => {
                openai_messages.push(ChatCompletionRequestMessage::System(
                    ChatCompletionRequestSystemMessageArgs::default()
                        .content(msg.content.clone())
                        .build()
                        .map_err(|e| {
                            KowalskiError::Initialization(format!("OpenAI message error: {}", e))
                        })?,
                ));
            }
            "user" => {
                openai_messages.push(ChatCompletionRequestMessage::User(
                    ChatCompletionRequestUserMessageArgs::default()
                        .content(msg.content.clone())
                        .build()
                        .map_err(|e| {
                            KowalskiError::Initialization(format!("OpenAI message error: {}", e))
                        })?,
                ));
            }
            "assistant" => {
                let mut args = ChatCompletionRequestAssistantMessageArgs::default();
                if !msg.content.is_empty() {
                    args.content(msg.content.clone());
                }
                if let Some(ref calls) = msg.tool_calls {
                    args.tool_calls(tool_calls_to_openai(calls));
                }
                openai_messages.push(ChatCompletionRequestMessage::Assistant(
                    args.build().map_err(|e| {
                        KowalskiError::Initialization(format!("OpenAI message error: {}", e))
                    })?,
                ));
            }
            "tool" => {
                let tool_call_id = msg.tool_call_id.clone().ok_or_else(|| {
                    KowalskiError::Initialization(
                        "Tool-role message has no tool_call_id (required by OpenAI-compatible APIs)"
                            .to_string(),
                    )
                })?;
                openai_messages.push(ChatCompletionRequestMessage::Tool(
                    ChatCompletionRequestToolMessageArgs::default()
                        .content(msg.content.clone())
                        .tool_call_id(tool_call_id)
                        .build()
                        .map_err(|e| {
                            KowalskiError::Initialization(format!("OpenAI message error: {}", e))
                        })?,
                ));
            }
            _ => {
                openai_messages.push(ChatCompletionRequestMessage::User(
                    ChatCompletionRequestUserMessageArgs::default()
                        .content(format!("[{}] {}", msg.role, msg.content))
                        .build()
                        .map_err(|e| {
                            KowalskiError::Initialization(format!("OpenAI message error: {}", e))
                        })?,
                ));
            }
        }
    }
    Ok(openai_messages)
}

/// Build a strict-json_schema Chat Completions request. Runs the conservative-subset guard
/// before anything touches the wire, so an out-of-subset schema fails with the offending
/// feature named instead of an opaque backend rejection.
fn build_schema_request(
    model: &str,
    messages: &[Message],
    schema: &serde_json::Value,
) -> Result<CreateChatCompletionRequest, KowalskiError> {
    ensure_schema_supported(schema)?;
    let openai_messages = messages_to_openai(messages)?;
    CreateChatCompletionRequestArgs::default()
        .model(model)
        .messages(openai_messages)
        .response_format(ResponseFormat::JsonSchema {
            json_schema: ResponseFormatJsonSchema {
                description: None,
                name: schema_wire_name(schema),
                schema: schema.clone(),
                strict: Some(true),
            },
        })
        .build()
        .map_err(|e| KowalskiError::Initialization(format!("OpenAI request error: {}", e)))
}

fn tool_defs_to_openai(tools: &[ToolDefinition]) -> Vec<ChatCompletionTools> {
    tools
        .iter()
        .map(|def| {
            ChatCompletionTools::Function(ChatCompletionTool {
                function: FunctionObject {
                    name: def.name.clone(),
                    description: Some(def.description.clone()),
                    parameters: Some(def.parameters.clone()),
                    strict: None,
                },
            })
        })
        .collect()
}

/// OpenAI carries function arguments as a JSON **string**; our [`ToolCall`] carries a
/// [`serde_json::Value`] — these two convert between the representations.
fn tool_calls_to_openai(calls: &[ToolCall]) -> Vec<ChatCompletionMessageToolCalls> {
    calls
        .iter()
        .map(|call| {
            ChatCompletionMessageToolCalls::Function(ChatCompletionMessageToolCall {
                id: call.id.clone(),
                function: OpenAIFunctionCall {
                    name: call.function.name.clone(),
                    arguments: call.function.arguments.to_string(),
                },
            })
        })
        .collect()
}

fn tool_calls_from_openai(calls: &[ChatCompletionMessageToolCalls]) -> Vec<ToolCall> {
    calls
        .iter()
        .filter_map(|call| match call {
            ChatCompletionMessageToolCalls::Function(f) => Some(ToolCall {
                id: f.id.clone(),
                function: FunctionCall {
                    name: f.function.name.clone(),
                    arguments: serde_json::from_str(&f.function.arguments).unwrap_or_else(|_| {
                        serde_json::Value::String(f.function.arguments.clone())
                    }),
                },
            }),
            ChatCompletionMessageToolCalls::Custom(_) => None,
        })
        .collect()
}

/// Map a Chat Completions response to a [`ChatOutcome`]; `None` when the first choice has
/// neither text nor tool calls.
fn outcome_from_response(response: &CreateChatCompletionResponse) -> Option<ChatOutcome> {
    let message = &response.choices.first()?.message;
    if let Some(ref raw_calls) = message.tool_calls {
        let calls = tool_calls_from_openai(raw_calls);
        if !calls.is_empty() {
            return Some(ChatOutcome::ToolCalls {
                content: message.content.clone().filter(|c| !c.is_empty()),
                calls,
            });
        }
    }
    message.content.clone().map(ChatOutcome::Text)
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn weather_tool() -> ToolDefinition {
        ToolDefinition {
            name: "get_current_weather".to_string(),
            description: "Get the current weather for a location".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "location": { "type": "string", "description": "The city name" }
                },
                "required": ["location"]
            }),
        }
    }

    #[test]
    fn request_with_tools_serializes_function_format() {
        let messages =
            messages_to_openai(&[Message::text("user", "What is the weather in Paris?")]).unwrap();
        let request = CreateChatCompletionRequestArgs::default()
            .model("gpt-4o-mini")
            .messages(messages)
            .tools(tool_defs_to_openai(&[weather_tool()]))
            .build()
            .unwrap();
        let wire = serde_json::to_value(&request).unwrap();

        let tools = wire["tools"].as_array().expect("tools array on the wire");
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0]["type"], "function");
        assert_eq!(tools[0]["function"]["name"], "get_current_weather");
        assert_eq!(
            tools[0]["function"]["parameters"]["required"],
            json!(["location"])
        );
    }

    #[test]
    fn tool_role_and_assistant_tool_calls_wire_format() {
        let history = vec![
            Message::text("user", "What is the weather in Paris?"),
            Message::assistant_tool_calls(
                "",
                vec![ToolCall {
                    id: "call_abc123".to_string(),
                    function: FunctionCall {
                        name: "get_current_weather".to_string(),
                        arguments: json!({"location": "Paris"}),
                    },
                }],
            ),
            Message::tool_result("call_abc123", "12 degrees and cloudy"),
        ];
        let wire = serde_json::to_value(messages_to_openai(&history).unwrap()).unwrap();

        assert_eq!(wire[1]["role"], "assistant");
        assert_eq!(wire[1]["tool_calls"][0]["type"], "function");
        assert_eq!(wire[1]["tool_calls"][0]["id"], "call_abc123");
        assert_eq!(
            wire[1]["tool_calls"][0]["function"]["name"],
            "get_current_weather"
        );
        // Arguments cross the wire as a JSON string, per the Chat Completions contract.
        let args: serde_json::Value = serde_json::from_str(
            wire[1]["tool_calls"][0]["function"]["arguments"]
                .as_str()
                .unwrap(),
        )
        .unwrap();
        assert_eq!(args, json!({"location": "Paris"}));

        assert_eq!(wire[2]["role"], "tool");
        assert_eq!(wire[2]["content"], "12 degrees and cloudy");
        assert_eq!(wire[2]["tool_call_id"], "call_abc123");
    }

    #[test]
    fn tool_role_message_without_call_id_is_an_error() {
        let mut bad = Message::tool_result("x", "result");
        bad.tool_call_id = None;
        assert!(messages_to_openai(&[bad]).is_err());
    }

    // Captured Chat Completions wire fixture: model answering with a tool call.
    #[test]
    fn response_with_tool_calls_parses_structured() {
        let fixture = r#"{
            "id": "chatcmpl-abc123",
            "object": "chat.completion",
            "created": 1770000000,
            "model": "gpt-4o-mini",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_abc123",
                        "type": "function",
                        "function": {
                            "name": "get_current_weather",
                            "arguments": "{\"location\": \"Paris\"}"
                        }
                    }]
                },
                "finish_reason": "tool_calls"
            }],
            "usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15}
        }"#;
        let response: CreateChatCompletionResponse = serde_json::from_str(fixture).unwrap();
        match outcome_from_response(&response).unwrap() {
            ChatOutcome::ToolCalls { content, calls } => {
                assert_eq!(content, None);
                assert_eq!(calls.len(), 1);
                assert_eq!(calls[0].id, "call_abc123");
                assert_eq!(calls[0].function.name, "get_current_weather");
                assert_eq!(calls[0].function.arguments, json!({"location": "Paris"}));
            }
            other => panic!("expected ToolCalls, got {:?}", other),
        }
    }

    #[test]
    fn response_without_tool_calls_parses_text() {
        let fixture = r#"{
            "id": "chatcmpl-abc124",
            "object": "chat.completion",
            "created": 1770000000,
            "model": "gpt-4o-mini",
            "choices": [{
                "index": 0,
                "message": { "role": "assistant", "content": "It is sunny." },
                "finish_reason": "stop"
            }]
        }"#;
        let response: CreateChatCompletionResponse = serde_json::from_str(fixture).unwrap();
        match outcome_from_response(&response).unwrap() {
            ChatOutcome::Text(text) => assert_eq!(text, "It is sunny."),
            other => panic!("expected Text, got {:?}", other),
        }
    }

    #[test]
    fn native_tools_flag_gates_support() {
        assert!(!OpenAIProvider::new("k", None).supports_native_tools("gpt-4o-mini"));
        assert!(
            OpenAIProvider::new("k", None)
                .with_native_tools(true)
                .supports_native_tools("gpt-4o-mini")
        );
    }

    #[test]
    fn schema_request_serializes_strict_json_schema_response_format() {
        let schema = json!({
            "title": "Person",
            "type": "object",
            "additionalProperties": false,
            "required": ["name"],
            "properties": { "name": { "type": "string" } }
        });
        let request =
            build_schema_request("gpt-4o-mini", &[Message::text("user", "Who?")], &schema)
                .unwrap();
        let wire = serde_json::to_value(&request).unwrap();

        let rf = &wire["response_format"];
        assert_eq!(rf["type"], "json_schema");
        assert_eq!(rf["json_schema"]["name"], "Person");
        assert_eq!(rf["json_schema"]["strict"], true);
        assert_eq!(rf["json_schema"]["schema"], schema);
    }

    #[test]
    fn schema_request_rejects_out_of_subset_schema() {
        let schema = json!({ "type": "number", "multipleOf": 2 });
        let err = build_schema_request("gpt-4o-mini", &[Message::text("user", "go")], &schema)
            .unwrap_err();
        assert!(err.to_string().contains("multipleOf"), "err: {err}");
    }

    #[test]
    fn structured_output_flag_gates_support() {
        assert!(!OpenAIProvider::new("k", None).supports_structured_output("gpt-4o-mini"));
        assert!(
            OpenAIProvider::new("k", None)
                .with_structured_output(true)
                .supports_structured_output("gpt-4o-mini")
        );
    }
}