modelplease 0.1.0

Provider-neutral language model client with streaming, media, capabilities, and optional provider integrations
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
// Copyright 2026 Thomas Santerre and Moderately AI Inc.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! OpenAI Chat Completions API implementation of [`LanguageModelProvider`].
//!
//! Also works with any OpenAI-compatible endpoint (vLLM, Azure OpenAI, etc.) via the
//! `base_url` configuration. Ollama is its own provider impl with adjusted defaults; MLX uses
//! this provider directly.
//!
//! Wire-format helpers (request building, response parsing, SSE handling, content-part
//! translation, error mapping) live in [`crate::openai_wire`] and are shared with
//! [`crate::bedrock_mantle::BedrockMantleProvider`]'s Chat Completions surface.

use std::{
    collections::{BTreeMap, HashMap},
    pin::Pin,
    sync::{Arc, LazyLock},
    time::Duration,
};

use async_trait::async_trait;
use enumset::enum_set;
use futures::stream::{Stream, StreamExt};
use moka::future::Cache;
use serde::Deserialize;

use crate::{
    capabilities::{
        MediaKind, MediaSupport, ModelCapabilities, ReasoningCapability, ReasoningMode,
        ReasoningParamConflicts,
    },
    config::ReasoningEffort,
    error::LanguageModelError,
    identifiers::{ApiKey, ModelId},
    media::SourceKind,
    openai_wire::{
        OpenAiResponse, OpenAiStreamOptions, build_request, convert_openai_sse_event,
        map_openai_error, parse_openai_response,
    },
    provider::{ChatModelInfo, GenerateRequest, LanguageModelProvider, ResponseFormatKind},
    response::{LanguageModelResponse, StreamDelta},
    retry::{RetryConfig, with_retry},
    sse::parse_sse_stream,
};

/// OpenAI Chat Completions API language model.
///
/// Calls `POST {base_url}/chat/completions`. Works with OpenAI directly or any OpenAI-compatible
/// endpoint via [`OpenAiConfig::base_url`].
///
/// # Compatible Providers
///
/// | Provider | Base URL | Start command |
/// |----------|----------|---------------|
/// | OpenAI | `https://api.openai.com/v1` (default) | — |
/// | MLX (text) | `http://localhost:8080/v1` | `mlx_lm.server --model <model>` |
/// | MLX (vision) | `http://localhost:8080/v1` | `mlx_vlm.server --model <model>` |
/// | vLLM | `http://<host>/v1` | `vllm serve <model>` |
///
/// The Ollama daemon has its own provider impl ([`crate::OllamaLanguageModel`]) with
/// reasoning-effort defaults that differ from OpenAI's.
pub struct OpenAiLanguageModel {
    client: Arc<reqwest::Client>,
    api_key: String,
    base_url: String,
    retry_config: RetryConfig,
    /// Single-entry TTL cache for [`LanguageModelProvider::list_models`].
    /// `time_to_live = 1h`, `max_capacity = 1`. moka's `try_get_with`
    /// coalesces concurrent misses.
    list_models_cache: Cache<(), Vec<ChatModelInfo>>,
}

/// Configuration for [`OpenAiLanguageModel`].
///
/// One provider instance serves every model the upstream supports — the model identifier arrives
/// in [`GenerateRequest`] per call. Override `base_url` for OpenAI-compatible providers (MLX,
/// vLLM, Azure).
pub struct OpenAiConfig {
    pub api_key: ApiKey,
    pub base_url: String,
    pub retry_config: RetryConfig,
}

impl OpenAiConfig {
    pub const DEFAULT_BASE_URL: &'static str = "https://api.openai.com/v1";
}

/// Injected dependencies for [`OpenAiLanguageModel`].
///
/// The HTTP client is constructed once at the application composition root
/// and cloned into every HTTP-based provider so the
/// macOS reqwest system-proxy trap fires at most once per process and the connection pool is
/// shared.
pub struct OpenAiDeps {
    pub client: Arc<reqwest::Client>,
}

impl OpenAiLanguageModel {
    /// Create a new OpenAI language model.
    #[must_use]
    pub fn new(deps: OpenAiDeps, config: OpenAiConfig) -> Self {
        Self {
            client: deps.client,
            api_key: config.api_key.into_string(),
            base_url: config.base_url,
            retry_config: config.retry_config,
            list_models_cache: Cache::builder()
                .time_to_live(Duration::from_secs(3600))
                .max_capacity(1)
                .build(),
        }
    }

    /// Send the request and parse the response.
    async fn execute(
        &self,
        model: &str,
        messages: &[crate::message::Message],
        config: &crate::config::LanguageModelConfig,
    ) -> Result<LanguageModelResponse, LanguageModelError> {
        let request_body = build_request(
            model,
            messages,
            config,
            openai_uses_completion_tokens(model),
        );
        let url = format!("{}/chat/completions", self.base_url);

        let response = self
            .client
            .post(&url)
            .header("authorization", format!("Bearer {}", self.api_key))
            .header("content-type", "application/json")
            .json(&request_body)
            .send()
            .await
            .map_err(|e| LanguageModelError::provider(e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let headers = response.headers().clone();
            let body =
                crate::http_error::read_error_body_or_warn(response, "openai", status.as_u16())
                    .await;
            return Err(map_openai_error(status.as_u16(), &body, &headers));
        }

        let api_response: OpenAiResponse = response
            .json()
            .await
            .map_err(|e| LanguageModelError::provider(format!("failed to parse response: {e}")))?;
        parse_openai_response(api_response)
    }

    /// Shared streaming helper for [`LanguageModelProvider::generate_stream`].
    async fn stream(
        &self,
        model: &str,
        messages: &[crate::message::Message],
        config: &crate::config::LanguageModelConfig,
    ) -> Result<
        Pin<Box<dyn Stream<Item = Result<StreamDelta, LanguageModelError>> + Send>>,
        LanguageModelError,
    > {
        let mut request_body = build_request(
            model,
            messages,
            config,
            openai_uses_completion_tokens(model),
        );
        request_body.stream = Some(true);
        // Without this flag OpenAI streams every chunk with `usage: null` — the final pre-`[DONE]`
        // chunk only carries usage when the client opts in. See StreamDelta::usage docs for
        // per-provider semantics.
        request_body.stream_options = Some(OpenAiStreamOptions {
            include_usage: true,
        });
        let url = format!("{}/chat/completions", self.base_url);

        let response = self
            .client
            .post(&url)
            .header("authorization", format!("Bearer {}", self.api_key))
            .header("content-type", "application/json")
            .json(&request_body)
            .send()
            .await
            .map_err(|e| LanguageModelError::provider(e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let headers = response.headers().clone();
            let body =
                crate::http_error::read_error_body_or_warn(response, "openai", status.as_u16())
                    .await;
            return Err(map_openai_error(status.as_u16(), &body, &headers));
        }

        let byte_stream = response.bytes_stream();
        let sse_stream = parse_sse_stream(byte_stream);
        let delta_stream = sse_stream
            .filter_map(|event_result| async move { convert_openai_sse_event(event_result) });
        Ok(Box::pin(delta_stream))
    }

    /// Fetch and merge the upstream model catalog for [`LanguageModelProvider::list_models`].
    ///
    /// Calls `GET {base_url}/models` (auth: `Authorization: Bearer {api_key}`). OpenAI's
    /// `/v1/models` returns IDs only — no context_window or capability flags — so per-ID metadata
    /// comes from the local [`MODEL_CAPABILITIES`] table. IDs missing from the local table fire a
    /// single `tracing::warn!` per cache fill.
    async fn fetch_and_merge_models(&self) -> Result<Vec<ChatModelInfo>, LanguageModelError> {
        let url = format!("{}/models", self.base_url);
        let response = self
            .client
            .get(&url)
            .header("authorization", format!("Bearer {}", self.api_key))
            .send()
            .await
            .map_err(|e| LanguageModelError::provider(e.to_string()))?;

        let status = response.status();
        if !status.is_success() {
            let headers = response.headers().clone();
            let body =
                crate::http_error::read_error_body_or_warn(response, "openai", status.as_u16())
                    .await;
            return Err(map_openai_error(status.as_u16(), &body, &headers));
        }

        let api_response: OpenAiListModelsResponse = response
            .json()
            .await
            .map_err(|e| LanguageModelError::provider(format!("failed to parse models: {e}")))?;

        let mut out = Vec::with_capacity(api_response.data.len());
        for entry in api_response.data {
            let caps = MODEL_CAPABILITIES.get(entry.id.as_str()).cloned();
            if caps.is_none() {
                tracing::warn!(
                    provider = "openai",
                    model_id = %entry.id,
                    "model returned by upstream but no local capability metadata; \
                     ChatModelInfo will have minimal fields. \
                     Update the local capability table when ready."
                );
            }
            // OpenAI accepts JsonObject everywhere it accepts JsonSchema, so the schema flag
            // implies both. Older models without JsonSchema typically still support JsonObject;
            // keep that conservative until we add a separate flag.
            let mut formats = vec![ResponseFormatKind::Text];
            if caps.as_ref().is_some_and(|c| c.supports_json_schema) {
                formats.push(ResponseFormatKind::JsonObject);
                formats.push(ResponseFormatKind::JsonSchema);
            }
            out.push(ChatModelInfo {
                id: ModelId::new(entry.id),
                display_name: None,
                context_window: caps.as_ref().map(|c| c.context_window),
                supports_streaming: caps.as_ref().is_some_and(|c| c.supports_streaming),
                supported_response_formats: formats,
                media_support: caps.as_ref().map(openai_media_support).unwrap_or_default(),
                reasoning: caps.as_ref().and_then(|c| c.reasoning.clone()),
            });
        }
        Ok(out)
    }
}

#[async_trait]
impl LanguageModelProvider for OpenAiLanguageModel {
    fn name(&self) -> &'static str {
        "openai"
    }

    fn capabilities(&self, model: &ModelId) -> Option<ModelCapabilities> {
        let caps = MODEL_CAPABILITIES.get(model.as_str())?;
        Some(ModelCapabilities {
            model_id: model.as_str().to_owned(),
            media_support: openai_media_support(caps),
            reasoning: caps.reasoning.clone(),
            // Latency-optimized inference is a Bedrock-only tier.
            latency_optimized_supported: false,
            // Our `cache_ttl` knob targets Bedrock/Anthropic; OpenAI caching is automatic, so the
            // extended-TTL tier doesn't apply.
            extended_cache_ttl_supported: false,
        })
    }

    #[tracing::instrument(skip(self), fields(provider = "openai", model_count = tracing::field::Empty), err(Display))]
    async fn list_models(&self) -> Result<Vec<ChatModelInfo>, LanguageModelError> {
        let result = self
            .list_models_cache
            .try_get_with((), self.fetch_and_merge_models())
            .await
            .map_err(|arc_err| (*arc_err).clone());
        if let Ok(ref v) = result {
            tracing::Span::current().record("model_count", v.len());
        }
        result
    }

    #[tracing::instrument(
        skip(self, request),
        fields(
            provider = "openai",
            model = %request.model,
            messages = request.messages.len(),
            prompt_tokens = tracing::field::Empty,
            completion_tokens = tracing::field::Empty,
            total_tokens = tracing::field::Empty,
            cache_creation_input_tokens = tracing::field::Empty,
            cache_read_input_tokens = tracing::field::Empty,
        ),
        err(Display),
    )]
    async fn generate(
        &self,
        request: GenerateRequest<'_>,
    ) -> Result<LanguageModelResponse, LanguageModelError> {
        self.validate_request(&request)?;
        let model = request.model.as_str();
        let response = with_retry(&self.retry_config, || {
            self.execute(model, request.messages, request.config)
        })
        .await?;
        let span = tracing::Span::current();
        if let Some(usage) = &response.usage {
            span.record("prompt_tokens", usage.input_tokens);
            span.record("completion_tokens", usage.output_tokens);
            span.record("total_tokens", usage.input_tokens + usage.output_tokens);
            span.record(
                "cache_creation_input_tokens",
                usage.cache_creation_input_tokens,
            );
            span.record("cache_read_input_tokens", usage.cache_read_input_tokens);
        }
        Ok(response)
    }

    #[tracing::instrument(
        skip(self, request),
        fields(
            provider = "openai",
            model = %request.model,
            messages = request.messages.len(),
            first_token_ms = tracing::field::Empty,
            prompt_tokens = tracing::field::Empty,
            completion_tokens = tracing::field::Empty,
            total_tokens = tracing::field::Empty,
            cache_creation_input_tokens = tracing::field::Empty,
            cache_read_input_tokens = tracing::field::Empty,
        ),
        err(Display),
    )]
    async fn generate_stream(
        &self,
        request: GenerateRequest<'_>,
    ) -> Result<
        Pin<Box<dyn Stream<Item = Result<StreamDelta, LanguageModelError>> + Send>>,
        LanguageModelError,
    > {
        let started_at = std::time::Instant::now();
        self.validate_request(&request)?;
        let inner = self
            .stream(request.model.as_str(), request.messages, request.config)
            .await?;
        let wrapped =
            crate::streaming_timing::instrument_stream(tracing::Span::current(), started_at, inner);
        Ok(Box::pin(wrapped))
    }
}

/// Derive the media-support table for an OpenAI model.
///
/// Every entry in [`MODEL_CAPABILITIES`] today is a vision model (gpt-4o family, gpt-5 family,
/// o-series reasoning), so image + document support is the default. Audio-only models
/// (`gpt-audio*`, `gpt-4o-audio-preview`) are not yet seeded in the capability table — they need
/// a separate `MediaKind::Audio` branch keyed off the model id when they are.
///
/// Source-kind notes:
/// - `Image` accepts `Url` (HTTPS) and `InlineBytes` (transmitted as a `data:` URI on the wire).
///   Chat Completions has no `file_id` primitive for images today — Files API images flow through
///   the Assistants API, not surfaced here.
/// - `Document` accepts `InlineBytes` and `ProviderFile` (Files API `file_id` via the `file`
///   content part).
fn openai_media_support(_caps: &ChatModelCapabilities) -> BTreeMap<MediaKind, MediaSupport> {
    let mut m = BTreeMap::new();
    m.insert(
        MediaKind::Image,
        MediaSupport {
            sources: enum_set!(SourceKind::Url | SourceKind::InlineBytes),
            formats: &["png", "jpeg", "webp", "gif"],
            // 20 MB upper bound per the OpenAI vision docs; enforce locally so callers don't waste
            // a round-trip on oversize base64 payloads.
            max_bytes: Some(20 * 1024 * 1024),
            max_count_per_message: None,
        },
    );
    m.insert(
        MediaKind::Document,
        MediaSupport {
            sources: enum_set!(SourceKind::InlineBytes | SourceKind::ProviderFile),
            // OpenAI's `file` content part accepts a broad set; PDFs are the well-tested format.
            // Subsequent passes can expand this.
            formats: &["pdf"],
            max_bytes: Some(50 * 1024 * 1024),
            max_count_per_message: None,
        },
    );
    m
}

/// Static capability table for OpenAI models.
///
/// `/v1/models` returns IDs only — context_window and capability flags come from this table.
/// New IDs returned upstream that aren't here fire a one-time warning per cache fill, prompting an
/// update.
#[derive(Debug, Clone)]
struct ChatModelCapabilities {
    context_window: u32,
    supports_streaming: bool,
    supports_json_schema: bool,
    /// `Some(_)` when the model accepts the `reasoning_effort` field. OpenAI doesn't expose a
    /// manual-budget knob, so every populated entry sets `supported_modes` to just `Adaptive`.
    reasoning: Option<ReasoningCapability>,
}

/// Sampling conflicts that apply to every OpenAI reasoning model.
///
/// OpenAI reasoning models (o-series + gpt-5 family) hard-reject any `temperature` value other
/// than the default `1.0` while `reasoning_effort` is set to a non-`none` level (live-tested
/// against the API). `top_p` has no documented restriction.
const fn openai_reasoning_conflicts() -> ReasoningParamConflicts {
    ReasoningParamConflicts {
        temperature_forbidden: true,
        top_k_forbidden: false,
        top_p_allowed_range: None,
    }
}

/// `ReasoningCapability` for the gpt-5 family — supports the full 5-value enum
/// (`none|low|medium|high|xhigh`) per OpenAI's published model spec for GPT-5.5 / GPT-5.4 /
/// GPT-5.4-mini.
const fn openai_gpt5_reasoning() -> ReasoningCapability {
    ReasoningCapability {
        supported_modes: enum_set!(ReasoningMode::Adaptive),
        supported_efforts: enum_set!(
            ReasoningEffort::None
                | ReasoningEffort::Low
                | ReasoningEffort::Medium
                | ReasoningEffort::High
                | ReasoningEffort::XHigh
        ),
        manual_budget_range: None,
        conflicts: openai_reasoning_conflicts(),
        sampling_params_removed: false,
    }
}

/// `ReasoningCapability` for the o-series (o1/o3/o4-mini family). Per OpenAI docs, accepts
/// `reasoning_effort: "low"|"medium"|"high"` — no `none` and no `xhigh` on these legacy reasoning
/// models.
const fn openai_o_series_reasoning() -> ReasoningCapability {
    ReasoningCapability {
        supported_modes: enum_set!(ReasoningMode::Adaptive),
        supported_efforts: enum_set!(
            ReasoningEffort::Low | ReasoningEffort::Medium | ReasoningEffort::High
        ),
        manual_budget_range: None,
        conflicts: openai_reasoning_conflicts(),
        sampling_params_removed: false,
    }
}

static MODEL_CAPABILITIES: LazyLock<HashMap<&'static str, ChatModelCapabilities>> =
    LazyLock::new(|| {
        let mut m = HashMap::new();
        // GPT-4o family — 128k context, full streaming + structured outputs. Non-reasoning models:
        // `reasoning_effort` is rejected by the API.
        for id in [
            "gpt-4o",
            "gpt-4o-mini",
            "gpt-4o-2024-11-20",
            "gpt-4o-2024-08-06",
        ] {
            m.insert(
                id,
                ChatModelCapabilities {
                    context_window: 128_000,
                    supports_streaming: true,
                    supports_json_schema: true,
                    reasoning: None,
                },
            );
        }
        // o-series reasoning models — 128k context, streaming, JsonSchema. Effort enum:
        // {low, medium, high} — the surface published with o1 and unchanged through o4-mini per
        // OpenAI's reasoning guide.
        for id in ["o1", "o1-mini", "o1-preview", "o3", "o3-mini", "o4-mini"] {
            m.insert(
                id,
                ChatModelCapabilities {
                    context_window: 128_000,
                    supports_streaming: true,
                    supports_json_schema: true,
                    reasoning: Some(openai_o_series_reasoning()),
                },
            );
        }
        // Legacy GPT-5 IDs preserved from prior workspace state — no reasoning surface declared.
        for id in ["gpt-5", "gpt-5-2025-08-07"] {
            m.insert(
                id,
                ChatModelCapabilities {
                    context_window: 256_000,
                    supports_streaming: true,
                    supports_json_schema: true,
                    reasoning: None,
                },
            );
        }
        // GPT-5.5 / GPT-5.4: per the OpenAI model spec, 1M context window, 128K max output,
        // accepts the full 5-value `reasoning_effort` enum.
        for id in ["gpt-5.5", "gpt-5.4"] {
            m.insert(
                id,
                ChatModelCapabilities {
                    context_window: 1_000_000,
                    supports_streaming: true,
                    supports_json_schema: true,
                    reasoning: Some(openai_gpt5_reasoning()),
                },
            );
        }
        // GPT-5.4-mini: same effort enum, 400K context per the spec.
        m.insert(
            "gpt-5.4-mini",
            ChatModelCapabilities {
                context_window: 400_000,
                supports_streaming: true,
                supports_json_schema: true,
                reasoning: Some(openai_gpt5_reasoning()),
            },
        );
        m
    });

/// Whether the given OpenAI model uses `max_completion_tokens` rather than the legacy
/// `max_tokens`. Driven by the local capability table: any model whose
/// `ChatModelCapabilities.reasoning` is `Some(_)` is on the new field. Unknown models fall back to
/// `max_tokens` for backwards-compat — surfacing a clear API error is preferable to a silent
/// override on an unrecognised model.
fn openai_uses_completion_tokens(model: &str) -> bool {
    MODEL_CAPABILITIES
        .get(model)
        .is_some_and(|c| c.reasoning.is_some())
}

// --- list_models serde structs ---

#[derive(Deserialize)]
struct OpenAiListModelsResponse {
    data: Vec<OpenAiListModelEntry>,
}

#[derive(Deserialize)]
struct OpenAiListModelEntry {
    id: String,
}

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

    #[test]
    fn capabilities_known_model_returns_image_support() {
        let lm = OpenAiLanguageModel::new(
            OpenAiDeps {
                client: Arc::new(reqwest::Client::new()),
            },
            OpenAiConfig {
                api_key: ApiKey::parse("test").unwrap(),
                base_url: OpenAiConfig::DEFAULT_BASE_URL.to_owned(),
                retry_config: RetryConfig::default(),
            },
        );
        let caps = lm.capabilities(&ModelId::new("gpt-4o")).unwrap();
        let image = caps.media_support.get(&MediaKind::Image).unwrap();
        assert!(image.sources.contains(SourceKind::Url));
        assert!(image.sources.contains(SourceKind::InlineBytes));
        // Chat-completions doesn't surface ProviderFile for images.
        assert!(!image.sources.contains(SourceKind::ProviderFile));
    }

    #[test]
    fn custom_base_url() {
        let lm = OpenAiLanguageModel::new(
            OpenAiDeps {
                client: Arc::new(reqwest::Client::new()),
            },
            OpenAiConfig {
                base_url: "https://my-vllm.example.com/v1".into(),
                api_key: ApiKey::parse("key").unwrap(),
                retry_config: RetryConfig::default(),
            },
        );
        assert_eq!(lm.base_url, "https://my-vllm.example.com/v1");
    }

    #[test]
    fn gpt5_family_uses_completion_tokens() {
        // gpt-5.x is in the cap table with a reasoning entry → uses_completion_tokens=true.
        assert!(openai_uses_completion_tokens("gpt-5.5"));
        assert!(openai_uses_completion_tokens("gpt-5.4"));
        assert!(openai_uses_completion_tokens("gpt-5.4-mini"));
        assert!(openai_uses_completion_tokens("o3-mini"));
        // gpt-4o is non-reasoning → legacy max_tokens.
        assert!(!openai_uses_completion_tokens("gpt-4o"));
        // Unknown model falls back to legacy max_tokens.
        assert!(!openai_uses_completion_tokens("some-future-model"));
    }
}