llmshim 0.12.0

Blazing fast LLM API translation layer in pure Rust
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
//! One schema walker with per-transport policy. External references are never
//! fetched; unresolved/cyclic schemas fall back per tool rather than per request.
mod budget;
mod memo;
pub mod validate;
mod walk;
use crate::error::Result;
pub(crate) use budget::{BudgetLimits, RequestBudget};
use serde_json::{json, Value};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Target {
    OpenAiChat,
    OpenAiResponses,
    Anthropic,
    Google,
    CloudCodeAssist,
    Mcp,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Nullable {
    Preserve,
    Marker,
    Remove,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Options {
    pub strict: bool,
    pub nullable: Nullable,
    pub rewrite_one_of: bool,
    pub strip_lookarounds: bool,
    pub target: Target,
    pub bypass: bool,
    pub max_depth: usize,
    pub max_nodes: usize,
    pub max_literal_bytes: usize,
}
impl Options {
    pub fn for_target(target: Target) -> Self {
        Self {
            strict: false,
            nullable: match target {
                Target::Google => Nullable::Marker,
                Target::CloudCodeAssist => Nullable::Remove,
                _ => Nullable::Preserve,
            },
            rewrite_one_of: matches!(
                target,
                Target::OpenAiResponses | Target::Google | Target::CloudCodeAssist
            ),
            strip_lookarounds: target == Target::OpenAiResponses,
            target,
            bypass: false,
            max_depth: 96,
            max_nodes: 32_768,
            max_literal_bytes: 8 * 1024 * 1024,
        }
    }
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Normalization {
    pub changed: bool,
    pub used_fallback: bool,
    pub strict: bool,
    pub bypassed: bool,
}
fn flag(name: &str) -> bool {
    std::env::var(name).is_ok_and(|v| v == "1" || v.eq_ignore_ascii_case("true"))
}

pub fn normalize_for(target: Target, schema: &mut Value) -> Normalization {
    normalize_with(&Options::for_target(target), schema)
}

pub fn normalize_with(options: &Options, schema: &mut Value) -> Normalization {
    normalize_with_optional_budget(options, schema, None)
        .expect("unbudgeted normalization cannot exhaust")
}

pub(crate) fn normalize_with_budget(
    options: &Options,
    schema: &mut Value,
    budget: &mut RequestBudget,
) -> Result<Normalization> {
    normalize_with_optional_budget(options, schema, Some(budget))
}

fn normalize_with_optional_budget(
    options: &Options,
    schema: &mut Value,
    mut budget: Option<&mut RequestBudget>,
) -> Result<Normalization> {
    if options.bypass || flag("LLMSHIM_NO_SCHEMA_NORMALIZATION") {
        return Ok(Normalization {
            bypassed: true,
            ..Normalization::default()
        });
    }
    let mut options = options.clone();
    if flag("LLMSHIM_NO_STRICT") {
        options.strict = false;
    }
    let key = (!flag("LLMSHIM_NO_SCHEMA_CACHE"))
        .then(|| memo::CACHE.key(&options, schema))
        .flatten();
    if let Some(key) = &key {
        if let Some(cached) = memo::CACHE.get(key, &options, schema) {
            if !cached.unchanged {
                if let Some(budget) = budget.as_deref_mut() {
                    budget.reserve_retained_footprint(cached.footprint)?;
                }
                *schema = cached.value.clone();
            }
            return Ok(cached.report);
        }
    }
    if let Some(budget) = budget.as_deref_mut() {
        budget.reserve_work(schema)?;
    }
    let input = std::mem::take(schema);
    let (value, report) = normalized_value(&options, &input);
    if let Some(budget) = budget {
        if let Err(error) = budget
            .reserve_work(&value)
            .and_then(|_| budget.reserve_retained(&value))
        {
            *schema = input;
            return Err(error);
        }
    }
    if let Some(key) = key {
        memo::CACHE.insert(key, options, input, &value, report);
    }
    *schema = value;
    Ok(report)
}

#[cfg(test)]
fn normalize_uncached(options: &Options, schema: &mut Value) -> Normalization {
    let original = std::mem::take(schema);
    let (value, report) = normalized_value(options, &original);
    *schema = value;
    report
}

fn normalized_value(options: &Options, original: &Value) -> (Value, Normalization) {
    match walk::normalize(original, options) {
        Ok(value) if validate::compile(&value).is_ok() => {
            let changed = value != *original;
            (
                value,
                Normalization {
                    changed,
                    strict: options.strict,
                    ..Normalization::default()
                },
            )
        }
        Ok(_) | Err(()) => {
            let value = json!({"type":"object","properties":{}});
            let changed = value != *original;
            (
                value,
                Normalization {
                    changed,
                    used_fallback: true,
                    ..Normalization::default()
                },
            )
        }
    }
}

/// Normalize an MCP tool as it enters a tool registry, before any model request.
pub fn normalize_mcp_tool(tool: &mut Value) -> Normalization {
    let Some(schema) = tool.get_mut("inputSchema") else {
        return Normalization::default();
    };
    let mut report = normalize_for(Target::Mcp, schema);
    if !report.bypassed && schema["type"] != "object" {
        *schema = json!({"type":"object","properties":{}});
        report.changed = true;
        report.used_fallback = true;
        report.strict = false;
    }
    report
}

/// Accept raw MCP tool definitions as well as canonical nested function tools.
pub(crate) fn prepare_request(request: &Value, budget: &mut RequestBudget) -> Result<Value> {
    budget.reserve_request_schemas(request)?;
    let mut request = request.clone();
    if let Some(tools) = request.get_mut("tools").and_then(Value::as_array_mut) {
        for tool in tools {
            if tool.get("inputSchema").is_none() {
                continue;
            }
            normalize_mcp_tool_with_budget(tool, budget)?;
            budget.reserve_retained(&tool["inputSchema"])?;
            let mut canonical = json!({"type":"function","function":{"name":tool["name"],"parameters":tool["inputSchema"]}});
            if let Some(description) = tool.get("description").or_else(|| tool.get("title")) {
                canonical["function"]["description"] = description.clone();
            }
            if let Some(cache) = tool.get("cache_control") {
                canonical["cache_control"] = cache.clone();
            }
            *tool = canonical;
        }
    }
    Ok(request)
}

fn normalize_mcp_tool_with_budget(
    tool: &mut Value,
    budget: &mut RequestBudget,
) -> Result<Normalization> {
    let Some(schema) = tool.get_mut("inputSchema") else {
        return Ok(Normalization::default());
    };
    let mut report = normalize_with_budget(&Options::for_target(Target::Mcp), schema, budget)?;
    if !report.bypassed && schema["type"] != "object" {
        let fallback = json!({"type":"object","properties":{}});
        budget.reserve_retained(&fallback)?;
        *schema = fallback;
        report.changed = true;
        report.used_fallback = true;
        report.strict = false;
    }
    Ok(report)
}

fn tool_schema(
    target: Target,
    tool: &mut Value,
    key: &str,
    budget: &mut RequestBudget,
) -> Result<()> {
    let requested = tool["strict"].as_bool().unwrap_or(false);
    let Some(schema) = tool.get_mut(key) else {
        return Ok(());
    };
    budget.reserve_retained(schema)?;
    let mut options = Options::for_target(target);
    options.strict = requested;
    let mut report = normalize_with_budget(&options, schema, budget)?;
    if !report.bypassed
        && (requested || matches!(target, Target::Google | Target::Anthropic))
        && schema["type"] != "object"
    {
        let fallback = json!({"type":"object","properties":{}});
        budget.reserve_retained(&fallback)?;
        *schema = fallback;
        report.used_fallback = true;
        report.strict = false;
    }
    if requested
        || (report.used_fallback && matches!(target, Target::OpenAiChat | Target::OpenAiResponses))
    {
        tool["strict"] = json!(report.strict);
    }
    Ok(())
}

/// A provider-ready output schema plus the reversible non-object wrapper.
#[derive(Debug, Clone)]
pub struct OutputSchema {
    pub schema: Value,
    pub wrapped: bool,
    pub normalization: Normalization,
}
impl OutputSchema {
    pub fn unwrap(&self, value: &Value) -> Option<Value> {
        if self.wrapped {
            value.get("response").cloned()
        } else {
            Some(value.clone())
        }
    }
}

/// Resolve the original document before wrapping so local references keep
/// their original root. Shims can validate the unwrapped value independently.
pub fn normalize_output_for(target: Target, original: &Value, strict: bool) -> OutputSchema {
    normalize_output_for_optional_budget(target, original, strict, None)
        .expect("unbudgeted normalization cannot exhaust")
}

pub(crate) fn normalize_output_for_budget(
    target: Target,
    original: &Value,
    strict: bool,
    budget: &mut RequestBudget,
) -> Result<OutputSchema> {
    normalize_output_for_optional_budget(target, original, strict, Some(budget))
}

fn normalize_output_for_optional_budget(
    target: Target,
    original: &Value,
    strict: bool,
    mut budget: Option<&mut RequestBudget>,
) -> Result<OutputSchema> {
    if let Some(budget) = budget.as_deref_mut() {
        budget.reserve_retained(original)?;
    }
    let mut schema = original.clone();
    let base = if let Some(budget) = budget.as_deref_mut() {
        normalize_with_budget(&Options::for_target(Target::Mcp), &mut schema, budget)?
    } else {
        normalize_for(Target::Mcp, &mut schema)
    };
    let wrapped = schema["type"] != "object";
    if wrapped {
        schema = json!({"type":"object","properties":{"response":schema},"required":["response"]});
        if let Some(budget) = budget.as_deref_mut() {
            budget.reserve_retained(&schema)?;
        }
    }
    let mut options = Options::for_target(target);
    options.strict = strict;
    let mut normalization = if let Some(budget) = budget {
        normalize_with_budget(&options, &mut schema, budget)?
    } else {
        normalize_with(&options, &mut schema)
    };
    normalization.used_fallback |= base.used_fallback;
    normalization.changed |= base.changed || wrapped;
    Ok(OutputSchema {
        schema,
        wrapped,
        normalization,
    })
}

/// Normalize native tool schemas after extension overrides. No user argument,
/// enum/default literal, or conversational content is recursively rewritten.
pub(crate) fn normalize_native_tools(
    target: Target,
    body: &mut Value,
    budget: &mut RequestBudget,
) -> Result<()> {
    if let Some(tools) = body.get_mut("tools").and_then(Value::as_array_mut) {
        for tool in tools {
            match target {
                Target::Google => {
                    if let Some(declarations) = tool
                        .get_mut("functionDeclarations")
                        .and_then(Value::as_array_mut)
                    {
                        for declaration in declarations {
                            tool_schema(target, declaration, "parameters", budget)?;
                            declaration.as_object_mut().unwrap().remove("strict");
                        }
                    }
                }
                Target::Anthropic => tool_schema(target, tool, "input_schema", budget)?,
                _ => {
                    if tool["function"].is_object() {
                        tool_schema(target, &mut tool["function"], "parameters", budget)?;
                    } else if tool["type"] == "function" {
                        tool_schema(target, tool, "parameters", budget)?;
                    }
                }
            }
        }
    }
    Ok(())
}

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

    fn limits(schema_copies: usize) -> BudgetLimits {
        BudgetLimits {
            schema_copies,
            retained_nodes: 10_000,
            retained_literal_bytes: 1_000_000,
            retained_owned_bytes: 1_000_000,
            work_passes: 100,
            work_nodes: 10_000,
            work_literal_bytes: 1_000_000,
            validators: 100,
            prompt_bytes: 1_000_000,
        }
    }

    fn assert_exhausted(error: crate::error::ShimError) {
        match error {
            crate::error::ShimError::ProviderError { status, body, .. } => {
                assert_eq!(status, 400);
                assert_eq!(body, "request schema budget exceeded");
            }
            error => panic!("unexpected error: {error:?}"),
        }
    }

    #[test]
    fn cached_normalized_values_reserve_each_returned_clone_before_assignment() {
        let input = json!({"type":"object","properties":{"request_budget_cache_fixture":{"type":"string","default":"value"}}});
        let mut options = Options::for_target(Target::OpenAiResponses);
        options.strict = true;
        let mut warmed = input.clone();
        assert!(normalize_with(&options, &mut warmed).changed);

        let mut budget = RequestBudget::with_limits(limits(2));
        let mut first = input.clone();
        normalize_with_budget(&options, &mut first, &mut budget).unwrap();
        let mut second = input.clone();
        normalize_with_budget(&options, &mut second, &mut budget).unwrap();
        assert_eq!(first, warmed);
        assert_eq!(second, warmed);

        let mut rejected = input.clone();
        let error = normalize_with_budget(&options, &mut rejected, &mut budget).unwrap_err();
        assert_exhausted(error);
        assert_eq!(rejected, input);
    }

    #[test]
    fn native_shapes_share_one_aggregate_budget_and_keep_per_schema_fallback() {
        let valid = json!({"type":"object","properties":{"value":{"type":"string"}}});
        let invalid = json!({"$ref":"#/missing"});
        let cases = [
            (
                Target::OpenAiChat,
                json!({"tools":[
                    {"type":"function","function":{"name":"a","strict":true,"parameters":invalid}},
                    {"type":"function","function":{"name":"b","strict":true,"parameters":valid}}
                ]}),
            ),
            (
                Target::OpenAiResponses,
                json!({"tools":[
                    {"type":"function","name":"a","strict":true,"parameters":invalid},
                    {"type":"function","name":"b","strict":true,"parameters":valid}
                ]}),
            ),
            (
                Target::Anthropic,
                json!({"tools":[
                    {"name":"a","input_schema":invalid},
                    {"name":"b","input_schema":valid}
                ]}),
            ),
            (
                Target::Google,
                json!({"tools":[{"functionDeclarations":[
                    {"name":"a","parameters":invalid},
                    {"name":"b","parameters":valid}
                ]}]}),
            ),
        ];

        for (target, mut body) in cases {
            let mut budget = RequestBudget::with_limits(limits(8));
            normalize_native_tools(target, &mut body, &mut budget).unwrap();
            let schemas: Vec<&Value> = match target {
                Target::OpenAiChat => body["tools"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|tool| &tool["function"]["parameters"])
                    .collect(),
                Target::OpenAiResponses => body["tools"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|tool| &tool["parameters"])
                    .collect(),
                Target::Anthropic => body["tools"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|tool| &tool["input_schema"])
                    .collect(),
                Target::Google => body["tools"][0]["functionDeclarations"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .map(|tool| &tool["parameters"])
                    .collect(),
                _ => unreachable!(),
            };
            assert_eq!(schemas[0], &json!({"type":"object","properties":{}}));
            assert_eq!(schemas[1]["type"], "object");
            assert!(schemas[1]["properties"].is_object());
            match target {
                Target::OpenAiChat => assert_eq!(body["tools"][0]["function"]["strict"], false),
                Target::OpenAiResponses => assert_eq!(body["tools"][0]["strict"], false),
                _ => {}
            }
        }

        let repeated_schema = json!({"type":"object","additional_properties":false});
        let mut warmed = repeated_schema.clone();
        assert!(normalize_for(Target::OpenAiResponses, &mut warmed).changed);
        let mut repeated = json!({"tools":[
            {"type":"function","name":"a","parameters":repeated_schema},
            {"type":"function","name":"b","parameters":repeated_schema}
        ]});
        let mut budget = RequestBudget::with_limits(limits(3));
        assert_exhausted(
            normalize_native_tools(Target::OpenAiResponses, &mut repeated, &mut budget)
                .unwrap_err(),
        );
    }

    #[test]
    fn bypass_and_output_schemas_cannot_escape_the_request_budget() {
        let schema = json!({"type":"object","properties":{"value":{"type":"string"}}});
        let mut bypassed = json!({"tools":[
            {"type":"function","name":"a","strict":true,"parameters":schema},
            {"type":"function","name":"b","strict":true,"parameters":schema}
        ]});
        let mut bypass_options = Options::for_target(Target::OpenAiResponses);
        bypass_options.bypass = true;
        let mut budget = RequestBudget::with_limits(limits(1));
        let first = bypassed["tools"][0].get_mut("parameters").unwrap();
        normalize_with_budget(&bypass_options, first, &mut budget).unwrap();
        budget.reserve_retained(first).unwrap();
        let second = bypassed["tools"][1].get_mut("parameters").unwrap();
        assert_exhausted(budget.reserve_retained(second).unwrap_err());

        let mut budget = RequestBudget::with_limits(limits(1));
        let output = normalize_output_for_budget(
            Target::OpenAiResponses,
            &json!({"type":"string"}),
            true,
            &mut budget,
        );
        assert_exhausted(output.unwrap_err());
    }

    #[test]
    fn raw_mcp_conversion_reserves_normalized_and_canonical_schema_copies() {
        let request = json!({"tools":[
            {"name":"a","inputSchema":{"type":"object","description":"mcp-a"}},
            {"name":"b","inputSchema":{"type":"object","description":"mcp-b"}}
        ]});
        let mut budget = RequestBudget::with_limits(limits(4));
        assert_exhausted(prepare_request(&request, &mut budget).unwrap_err());
    }

    #[test]
    fn delegated_provider_assembly_and_second_override_pass_reuse_the_same_budget() {
        let request = json!({
            "messages": [],
            "tools": [{"type":"function","function":{"name":"tool","parameters":{
                "type":"object","additional_properties":false
            }}}]
        });
        let provider = crate::providers::openai::OpenAi::new("test".into());
        let target = crate::reasoning::ReplayTarget::new(
            "openai",
            "model",
            crate::reasoning::WireFormat::OpenAiResponses,
        );
        let mut budget = RequestBudget::with_limits(limits(1));
        let error = provider
            .transform_request_for_target_with_budget("model", &request, &target, &mut budget)
            .err()
            .expect("provider assembly must reject the second retained schema copy");
        assert_exhausted(error);

        let schema = json!({"type":"object","additional_properties":false});
        let mut first_body =
            json!({"tools":[{"type":"function","name":"first","parameters":schema}]});
        let mut warmed = schema.clone();
        normalize_for(Target::OpenAiResponses, &mut warmed);
        let mut budget = RequestBudget::with_limits(limits(3));
        normalize_native_tools(Target::OpenAiResponses, &mut first_body, &mut budget).unwrap();
        let mut override_body =
            json!({"tools":[{"type":"function","name":"override","parameters":schema}]});
        assert_exhausted(
            normalize_native_tools(Target::OpenAiResponses, &mut override_body, &mut budget)
                .unwrap_err(),
        );
    }
}