rig-core 0.44.0

An opinionated library for building LLM powered applications.
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
//! The provider configuration and the dialect table.

use super::*;
use crate::wire::secret::tests::a_config_reloads_without_its_credential;

/// A recorded request (`"when"`) or reply (`"then"`) body from a cassette
/// under `crates/rig-cassette/fixtures/cassettes/openai/`.
///
/// Hand-rolled rather than YAML-parsed because `serde_yaml` is not a
/// dev-dependency of this crate, and a cassette is never edited: the two
/// scalar forms the recorder emits — a single-quoted one-liner for a JSON
/// body, a `|+` literal block for an SSE body — are the whole grammar.
pub(super) fn recorded(section: &str, relative: &str) -> String {
    let root = concat!(
        env!("CARGO_MANIFEST_DIR"),
        "/../rig-cassette/fixtures/cassettes/openai/"
    );
    let text = std::fs::read_to_string(format!("{root}{relative}"))
        .unwrap_or_else(|error| panic!("cassette {relative} is readable: {error}"));
    let (when, then) = text
        .split_once("\nthen:\n")
        .unwrap_or_else(|| panic!("cassette {relative} has a `then:` section"));
    let scope = if section == "when" { when } else { then };
    let mut lines = scope.lines();
    let body = lines
        .by_ref()
        .find(|line| line.trim_start().starts_with("body:"))
        .and_then(|line| line.split_once("body:"))
        .map(|(_, rest)| rest.trim())
        .unwrap_or_else(|| panic!("cassette {relative} names a {section} body"));
    if let Some(quoted) = body.strip_prefix('\'').and_then(|b| b.strip_suffix('\'')) {
        return quoted.replace("''", "'");
    }
    // A `|`/`|+` literal block: every following line is indented by four
    // spaces, and the block ends at the first line indented less than that.
    let mut block = String::new();
    for line in lines {
        if !line.is_empty() && !line.starts_with("    ") {
            break;
        }
        block.push_str(line.get(4..).unwrap_or_default());
        block.push('\n');
    }
    block
}

/// The recorded body, parsed as JSON.
pub(super) fn recorded_json(section: &str, relative: &str) -> serde_json::Value {
    serde_json::from_str(&recorded(section, relative))
        .unwrap_or_else(|error| panic!("cassette {relative} {section} body is JSON: {error}"))
}

/// A wire is data a host may store in a config file or a scene, so it must
/// be serializable — and serializing it must never write the credential.
#[test]
fn a_serialized_configuration_carries_no_key_material() {
    a_config_reloads_without_its_credential(
        &OpenAIConfig::new("sk-secret"),
        "sk-secret",
        |openai| &openai.api_key,
    );

    // And neither does a wire built from it, which is what a host actually
    // stores — on either completion endpoint.
    for wire in [
        serde_json::to_string(&OpenAIConfig::new("sk-secret").chat("gpt-5.2")),
        serde_json::to_string(&OpenAIConfig::new("sk-secret").responses("gpt-5.2")),
    ] {
        let json = wire.expect("the wire serializes");
        assert!(!json.contains("sk-secret"), "the key leaked: {json}");
    }
}

/// The gateway-specific fields of the one configuration — the account, the
/// instructions, the caller identity a Responses gateway asks for — travel
/// with it: a stored ChatGPT wire loads back as the same wire, minus the
/// credential.
#[test]
fn a_gateway_configuration_round_trips_without_its_credential() {
    let chatgpt = crate::providers::chatgpt::DIALECT;
    let config = OpenAIConfig::with_key(&chatgpt, "tok-secret").with_account_id("acct-1");
    assert_eq!(
        config.instructions.as_deref(),
        chatgpt.quirks.default_instructions
    );
    assert!(config.identity.is_some());

    let json = serde_json::to_string(&config).expect("serializes");
    assert!(!json.contains("tok-secret"), "the token leaked: {json}");
    let restored: OpenAIConfig = serde_json::from_str(&json).expect("deserializes");
    assert_eq!(restored.dialect, chatgpt);
    assert_eq!(restored.account_id.as_deref(), Some("acct-1"));
    assert_eq!(restored.instructions, config.instructions);
    assert_eq!(restored.identity, config.identity);
    assert!(restored.api_key.is_empty());

    let wire = config.responses("gpt-5.4");
    let restored: super::super::responses_api::wire::Responses =
        serde_json::from_str(&serde_json::to_string(&wire).expect("serializes"))
            .expect("deserializes");
    assert_eq!(restored.system_instructions, wire.system_instructions);
    assert_eq!(restored.provider.dialect, chatgpt);
}

/// The endpoint template a wire's encoded request declares.
fn route_of(wire: &OpenAiWire) -> Option<&'static str> {
    use crate::wire::Wire as _;
    let request = crate::completion::CompletionRequest::new("hi");
    wire.encode(request, crate::wire::Mode::Unary)
        .expect("the request encodes")
        .route
}

/// The default completion wire is the dialect's flagship route, and it is
/// data like the two wires it chooses between: it stores without the
/// credential and loads back onto the same route.
#[test]
fn the_default_completion_wire_is_the_dialects_route_and_round_trips() {
    use crate::wire::Wire as _;
    let openai = OpenAIConfig::new("sk-secret").completion("gpt-5.2");
    let groq = OpenAIConfig::with_key(&GROQ, "gsk-secret").completion("llama-3.3-70b-versatile");
    assert!(matches!(openai, OpenAiWire::Responses(_)), "{openai:?}");
    assert!(matches!(groq, OpenAiWire::Chat(_)), "{groq:?}");
    assert_eq!(route_of(&openai), Some("/responses"));
    assert_eq!(route_of(&groq), Some("/chat/completions"));

    for (wire, secret) in [(openai, "sk-secret"), (groq, "gsk-secret")] {
        let json = serde_json::to_string(&wire).expect("the wire serializes");
        assert!(!json.contains(secret), "the key leaked: {json}");
        let restored: OpenAiWire = serde_json::from_str(&json).expect("the wire deserializes");
        assert_eq!(restored.describe().model, wire.describe().model);
        assert_eq!(restored.provider().dialect, wire.provider().dialect);
        assert_eq!(
            std::mem::discriminant(&restored),
            std::mem::discriminant(&wire)
        );
    }
}

/// The endpoint is configuration: a route chosen once on the configuration
/// overrides the dialect's flagship for every completion it builds, and it
/// is stored beside the dialect rather than inside it — a dialect
/// serializes as its name alone, so an override written into its quirks
/// would not survive a round trip.
#[test]
fn a_configured_route_overrides_the_dialects_and_round_trips() {
    let on_chat = OpenAIConfig::new("sk-secret").with_route(Route::Chat);
    let on_responses = OpenAIConfig::with_key(&GROQ, "gsk-secret").with_route(Route::Responses);
    assert_eq!(on_chat.completion_route(), Route::Chat);
    assert_eq!(on_responses.completion_route(), Route::Responses);
    assert_eq!(
        route_of(&on_chat.completion("gpt-5.2")),
        Some("/chat/completions")
    );
    assert_eq!(
        route_of(&on_responses.completion("llama")),
        Some("/responses")
    );

    let restored: OpenAIConfig =
        serde_json::from_str(&serde_json::to_string(&on_chat).expect("serializes"))
            .expect("deserializes");
    assert_eq!(restored.completion_route(), Route::Chat);
    assert!(
        matches!(restored.completion("gpt-5.2"), OpenAiWire::Chat(_)),
        "the configured route survives storage"
    );
    // Without an override, the stored form names no route at all.
    let json = serde_json::to_string(&OpenAIConfig::new("sk-secret")).expect("serializes");
    assert!(!json.contains("route"), "{json}");
}

/// A dialect is an identity, so its wire format is its name.
#[test]
fn a_dialect_round_trips_through_its_name() {
    let json = serde_json::to_string(&GROQ).expect("a dialect serializes");
    assert_eq!(json, "\"groq\"");
    assert_eq!(
        serde_json::from_str::<Dialect>(&json).expect("a dialect deserializes"),
        GROQ
    );

    // Through a whole configuration, which is how it actually travels.
    let config = OpenAIConfig::new("k").with_dialect(&MISTRAL);
    let restored: OpenAIConfig =
        serde_json::from_str(&serde_json::to_string(&config).expect("serializes"))
            .expect("deserializes");
    assert_eq!(restored.dialect, MISTRAL);
    assert_eq!(restored.base_url, MISTRAL.base_url);
}

/// A name this build does not know is an error, not a half-constructed
/// provider pointed at nothing.
#[test]
fn an_unknown_dialect_name_is_rejected() {
    let error = serde_json::from_str::<Dialect>("\"not-a-provider\"")
        .expect_err("an unknown dialect is rejected");
    assert!(
        error.to_string().contains("not-a-provider"),
        "the error names the dialect: {error}"
    );
}

/// The credential goes in the header the dialect uses, and a local server
/// started without a key gets no `Authorization` header at all.
#[test]
fn the_dialect_decides_the_credential_header() {
    fn headers(provider: &OpenAIConfig) -> http::HeaderMap {
        provider
            .authenticate(http::Request::get("https://example.invalid/"))
            .body(())
            .expect("builds")
            .headers()
            .clone()
    }

    let openai = headers(&OpenAIConfig::new("sk-test"));
    assert_eq!(openai["authorization"], "Bearer sk-test");

    let azure = headers(&OpenAIConfig::with_key(&AZURE, "azure-key"));
    assert_eq!(azure["api-key"], "azure-key");
    assert!(!azure.contains_key("authorization"));

    let keyless = headers(&OpenAIConfig::with_key(&LLAMACPP, ""));
    assert!(
        !keyless.contains_key("authorization"),
        "`llama-server` rejects a request carrying a key it was not started with"
    );
    let keyed = headers(&OpenAIConfig::with_key(&LLAMACPP, "local"));
    assert_eq!(keyed["authorization"], "Bearer local");
}

/// A dialect with no token-free credential check says so, instead of
/// verifying against an endpoint that bills the caller.
#[test]
fn a_dialect_without_a_verify_endpoint_refuses_to_invent_one() {
    use crate::wire::{Mode, Wire};

    assert!(
        OpenAIConfig::with_key(&PERPLEXITY, "k")
            .verify()
            .encode((), Mode::Unary)
            .is_err()
    );
    assert!(
        OpenAIConfig::new("k")
            .verify()
            .encode((), Mode::Unary)
            .is_ok()
    );
}

/// Azure accepts an account key *or* an Entra bearer token, and they are not
/// two spellings of one credential: the key goes out as `api-key`, the token
/// as `Authorization: Bearer`.
#[test]
fn azure_accepts_either_credential_under_its_own_header() {
    fn headers(provider: &OpenAIConfig) -> http::HeaderMap {
        provider
            .authenticate(http::Request::get("https://example.invalid/"))
            .body(())
            .expect("builds")
            .headers()
            .clone()
    }

    // The dialect names the alternative, which is what `from_env_with` reads
    // when the primary variable is unset.
    let alternative = AZURE
        .alternate_auth
        .expect("azure accepts a second credential");
    assert_eq!(alternative.api_key_env, "AZURE_TOKEN");
    assert_eq!(alternative.auth, Auth::Bearer);
    assert_eq!(AZURE.api_key_env, "AZURE_API_KEY");

    let keyed = headers(&OpenAIConfig::with_key(&AZURE, "account-key"));
    assert_eq!(keyed["api-key"], "account-key");
    assert!(!keyed.contains_key("authorization"));

    let token = headers(&OpenAIConfig::with_alternate_key(&AZURE, "entra-token"));
    assert_eq!(token["authorization"], "Bearer entra-token");
    assert!(
        !token.contains_key("api-key"),
        "an Entra token is not an account key"
    );
}

/// Hugging Face's router picks a sub-provider, and the choice is observable:
/// Fireworks addresses models by a qualified id, and only the default
/// sub-provider serves the endpoints that put the model in the URL.
#[test]
fn the_huggingface_sub_route_decides_the_model_and_the_routes() {
    assert_eq!(
        SubRoute::Fireworks.model_identifier("llama-3.3-70b"),
        "accounts/fireworks/models/llama-3.3-70b"
    );
    // Idempotent: the rewrite runs on the resolved request model, so an
    // already-qualified per-request override must not be prefixed twice.
    assert_eq!(
        SubRoute::Fireworks.model_identifier("accounts/fireworks/models/llama-3.3-70b"),
        "accounts/fireworks/models/llama-3.3-70b"
    );
    assert_eq!(
        SubRoute::Together.model_identifier("llama-3.3-70b"),
        "llama-3.3-70b"
    );

    // The slugs the router routes by.
    assert_eq!(SubRoute::HFInference.slug(), "hf-inference/models");
    assert_eq!(SubRoute::Fireworks.slug(), "fireworks-ai");
    assert_eq!(SubRoute::from("my-route").slug(), "my-route");

    // `None` behaves as the router's own default, which is the only
    // sub-provider that serves the model-routed endpoints.
    let default = OpenAIConfig::with_key(&HUGGINGFACE, "hf");
    assert_eq!(
        default
            .modality_uri(
                "transcription",
                "/audio/transcriptions",
                "openai/whisper-large-v3"
            )
            .expect("hf-inference serves transcription"),
        "https://router.huggingface.co/openai/whisper-large-v3"
    );

    let routed = default.clone().with_sub_route(SubRoute::Together);
    let error = routed
        .modality_uri("transcription", "/audio/transcriptions", "whisper")
        .expect_err("only hf-inference serves transcription");
    assert_eq!(
        error,
        "transcription endpoint is not supported yet for together"
    );
    assert!(
        routed
            .modality_uri("image generation", "/images/generations", "sd")
            .is_err()
    );

    // A dialect that does not route keeps its fixed path.
    assert_eq!(
        OpenAIConfig::new("k")
            .modality_uri("transcription", "/audio/transcriptions", "whisper-1")
            .expect("openai serves transcription"),
        "https://api.openai.com/v1/audio/transcriptions"
    );
}

/// Every dialect's listing and credential-check URL, against the paths the
/// recorded fixtures under `crates/rig-cassette/fixtures/cassettes/<provider>/**` actually show.
///
/// This is the audit in executable form. The deleted client did things to a
/// request that each wire's `encode` must now state — `llama-server`'s
/// unversioned operational routes are the case that got through review once
/// (`verify_path: "/props"` resolved to `/v1/props`, which that server
/// answers 404) — so the paths are pinned rather than re-derived.
#[test]
fn every_dialects_listing_and_verify_urls_match_the_recorded_paths() {
    // (dialect, models URL, verify URL or None when the dialect offers no
    // token-free check)
    let expected: &[(&Dialect, &str, Option<&str>)] = &[
        (
            &OPENAI,
            "https://api.openai.com/v1/models",
            Some("https://api.openai.com/v1/models"),
        ),
        (
            &DEEPSEEK,
            "https://api.deepseek.com/models",
            Some("https://api.deepseek.com/user/balance"),
        ),
        (
            &GROQ,
            "https://api.groq.com/openai/v1/models",
            Some("https://api.groq.com/openai/v1/models"),
        ),
        (
            &MISTRAL,
            "https://api.mistral.ai/v1/models",
            Some("https://api.mistral.ai/v1/models"),
        ),
        (
            &OPENROUTER,
            "https://openrouter.ai/api/v1/models",
            Some("https://openrouter.ai/api/v1/key"),
        ),
        (
            &VENICE,
            "https://api.venice.ai/api/v1/models",
            Some("https://api.venice.ai/api/v1/models"),
        ),
        (
            // The one that was wrong: `/props` is a root route on
            // `llama-server`, while `/models` is versioned.
            &LLAMACPP,
            "http://localhost:8080/v1/models",
            Some("http://localhost:8080/props"),
        ),
        (
            &MIRA,
            "https://api.mira.network/v1/models",
            Some("https://api.mira.network/user-credits"),
        ),
        (
            &HUGGINGFACE,
            "https://router.huggingface.co/models",
            Some("https://router.huggingface.co/api/whoami-v2"),
        ),
        (
            &TOGETHER,
            "https://api.together.xyz/v1/models",
            Some("https://api.together.xyz/models"),
        ),
        (&PERPLEXITY, "https://api.perplexity.ai/models", None),
        (
            &crate::providers::xai::DIALECT,
            "https://api.x.ai/v1/models",
            Some("https://api.x.ai/v1/api-key"),
        ),
    ];

    for (dialect, models, verify) in expected {
        let provider = OpenAIConfig::with_key(dialect, "k");
        assert_eq!(
            provider.uri(dialect.quirks.models_path, None),
            *models,
            "{} model-listing URL",
            dialect.name
        );
        match verify {
            Some(verify) => assert_eq!(
                provider.uri(dialect.quirks.verify_path, None),
                *verify,
                "{} verify URL",
                dialect.name
            ),
            None => assert!(
                dialect.quirks.verify_path.is_empty(),
                "{} has no token-free credential check",
                dialect.name
            ),
        }
    }
}

/// `llama-server` serves its operational routes at the root; the versioned
/// base URL applies to everything else.
#[test]
fn llamacpp_serves_its_operational_routes_unversioned() {
    let provider = OpenAIConfig::with_key(&LLAMACPP, "");
    for route in LLAMACPP.quirks.root_relative_routes {
        assert_eq!(
            provider.uri(route, None),
            format!("http://localhost:8080{route}"),
            "{route} is a root route"
        );
    }
    // Everything else keeps the `/v1` the base URL carries.
    assert_eq!(
        provider.uri("/chat/completions", None),
        "http://localhost:8080/v1/chat/completions"
    );
    assert_eq!(
        provider.uri("/rerank", None),
        "http://localhost:8080/v1/rerank"
    );
    // And no other dialect has any.
    for dialect in all() {
        if dialect.name != "llamacpp" {
            assert!(
                dialect.quirks.root_relative_routes.is_empty(),
                "{} declares root routes it never had",
                dialect.name
            );
        }
    }
}