everruns-provider 0.22.0

Provider/LLM abstraction foundation shared by Everruns core and provider crates
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
//! Provider-specific JSON Schema compatibility at the LLM request boundary.

use serde_json::Value;

const ZOD_EMAIL_PATTERN: &str = r"^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+\-\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$";
const OPENAI_SAFE_ZOD_EMAIL_PATTERN: &str = r"^[A-Za-z0-9_'+\-](?:[A-Za-z0-9_'+\-]|\.[A-Za-z0-9_'+\-])*@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$";
const SERVER_VALIDATION_NOTE: &str = "Validation for this value is enforced by the tool.";

/// Make a tool schema acceptable to OpenAI without changing compatible constraints.
///
/// OpenAI rejects ECMAScript lookaround in `pattern`. The common Zod email
/// pattern has a lookaround-free equivalent. Other unsupported patterns cannot
/// be translated soundly in general, so they are omitted from the model-facing
/// copy only; the MCP server remains the validation boundary.
pub fn sanitize_openai_tool_schema(schema: &Value) -> Value {
    let mut sanitized = schema.clone();
    sanitize_node(&mut sanitized);
    sanitized
}

/// Normalize a tool schema for OpenAI strict structured outputs.
///
/// Returns `None` when the schema uses constructs whose object shape cannot be
/// closed without risking a semantic change. Callers must then use the
/// sanitized non-strict schema instead.
pub fn strict_openai_tool_schema(schema: &Value) -> Option<Value> {
    let mut normalized = sanitize_openai_tool_schema(schema);
    if normalized.get("type").and_then(Value::as_str) != Some("object") {
        return None;
    }
    normalize_strict_node(&mut normalized)?;
    Some(normalized)
}

fn normalize_strict_node(node: &mut Value) -> Option<()> {
    let object = node.as_object_mut()?;

    // Strict structured outputs support only a JSON Schema subset. Decline
    // unknown keywords rather than sending a request the provider may reject.
    if object.keys().any(|key| {
        !matches!(
            key.as_str(),
            "type"
                | "properties"
                | "required"
                | "additionalProperties"
                | "items"
                | "enum"
                | "anyOf"
                | "description"
                | "title"
        )
    }) {
        return None;
    }

    if let Some(any_of) = object.get_mut("anyOf") {
        let variants = any_of.as_array_mut()?;
        if variants.is_empty() {
            return None;
        }
        for variant in variants {
            normalize_strict_node(variant)?;
        }
    }

    let schema_type = object.get("type").cloned();
    let types = match schema_type.as_ref() {
        Some(Value::String(value)) => vec![value.as_str()],
        Some(Value::Array(values)) if !values.is_empty() => values
            .iter()
            .map(Value::as_str)
            .collect::<Option<Vec<_>>>()?,
        None if object.contains_key("anyOf") || object.contains_key("enum") => Vec::new(),
        _ => return None,
    };

    let is_object = types.contains(&"object");
    let is_array = types.contains(&"array");
    if (object.contains_key("properties") || object.contains_key("required")) && !is_object {
        return None;
    }
    if object.contains_key("items") && !is_array {
        return None;
    }

    if is_object {
        // Explicitly open/dictionary shapes cannot be closed without removing
        // valid entries. Preserve them through the caller's non-strict fallback.
        if object
            .get("additionalProperties")
            .is_some_and(|value| value != &Value::Bool(false))
        {
            return None;
        }
        // A union containing object and another non-null type has no unambiguous
        // place for object-only strict constraints.
        if types
            .iter()
            .any(|value| *value != "object" && *value != "null")
        {
            return None;
        }
        let originally_required: std::collections::HashSet<String> = object
            .get("required")
            .map(|required| required.as_array().map(Vec::as_slice))
            .unwrap_or(Some(&[]))?
            .iter()
            .map(|value| value.as_str().map(str::to_owned))
            .collect::<Option<_>>()?;
        let property_names = {
            let properties = object
                .entry("properties")
                .or_insert_with(|| Value::Object(serde_json::Map::new()))
                .as_object_mut()?;
            if originally_required
                .iter()
                .any(|name| !properties.contains_key(name))
            {
                return None;
            }

            for (name, property) in properties.iter_mut() {
                normalize_strict_node(property)?;
                if !originally_required.contains(name) {
                    make_nullable(property)?;
                }
            }
            properties.keys().cloned().map(Value::String).collect()
        };
        object.insert("required".to_string(), Value::Array(property_names));
        object.insert("additionalProperties".to_string(), Value::Bool(false));
    }

    if is_array {
        if types
            .iter()
            .any(|value| *value != "array" && *value != "null")
        {
            return None;
        }
        normalize_strict_node(object.get_mut("items")?)?;
    }

    Some(())
}

fn make_nullable(schema: &mut Value) -> Option<()> {
    let object = schema.as_object_mut()?;
    // Type and enum constraints both apply: adding null only to `type` still
    // leaves an optional enum impossible to omit in strict mode.
    if let Some(values) = object.get_mut("enum") {
        let values = values.as_array_mut()?;
        if !values.contains(&Value::Null) {
            values.push(Value::Null);
        }
    }
    if let Some(schema_type) = object.get_mut("type") {
        match schema_type {
            Value::String(value) if value != "null" => {
                *schema_type = Value::Array(vec![
                    Value::String(value.clone()),
                    Value::String("null".into()),
                ]);
            }
            Value::String(_) => {}
            Value::Array(types) => {
                if !types.iter().all(Value::is_string) {
                    return None;
                }
                if !types.iter().any(|value| value.as_str() == Some("null")) {
                    types.push(Value::String("null".into()));
                }
            }
            _ => return None,
        }
        return Some(());
    }

    let variants = object.get_mut("anyOf")?.as_array_mut()?;
    if !variants
        .iter()
        .any(|variant| variant.get("type").and_then(Value::as_str) == Some("null"))
    {
        variants.push(serde_json::json!({"type": "null"}));
    }
    Some(())
}

fn sanitize_node(node: &mut Value) {
    match node {
        Value::Object(object) => {
            let pattern = object
                .get("pattern")
                .and_then(Value::as_str)
                .map(str::to_string);
            if let Some(pattern) = pattern {
                if pattern == ZOD_EMAIL_PATTERN {
                    object.insert(
                        "pattern".to_string(),
                        Value::String(OPENAI_SAFE_ZOD_EMAIL_PATTERN.to_string()),
                    );
                } else if contains_regex_lookaround(&pattern) {
                    object.remove("pattern");
                    let description = object
                        .get("description")
                        .and_then(Value::as_str)
                        .map(|description| format!("{description} {SERVER_VALIDATION_NOTE}"))
                        .unwrap_or_else(|| SERVER_VALIDATION_NOTE.to_string());
                    object.insert("description".to_string(), Value::String(description));
                }
            }
            // Recurse only into schema-valued keywords. Objects in enum,
            // default, const, examples or extension data are literal payloads.
            for (keyword, value) in object.iter_mut() {
                match keyword.as_str() {
                    "properties" | "patternProperties" | "$defs" | "definitions"
                    | "dependentSchemas" | "dependencies" => {
                        if let Some(schemas) = value.as_object_mut() {
                            for schema in schemas.values_mut() {
                                sanitize_node(schema);
                            }
                        }
                    }
                    "items"
                    | "additionalItems"
                    | "additionalProperties"
                    | "contains"
                    | "propertyNames"
                    | "not"
                    | "if"
                    | "then"
                    | "else"
                    | "unevaluatedProperties"
                    | "unevaluatedItems"
                    | "contentSchema"
                    | "allOf"
                    | "anyOf"
                    | "oneOf"
                    | "prefixItems" => sanitize_node(value),
                    _ => {}
                }
            }
        }
        Value::Array(items) => {
            for item in items {
                sanitize_node(item);
            }
        }
        _ => {}
    }
}

fn contains_regex_lookaround(pattern: &str) -> bool {
    let bytes = pattern.as_bytes();
    let mut escaped = false;
    let mut in_character_class = false;
    let mut index = 0;
    while index < bytes.len() {
        let byte = bytes[index];
        if escaped {
            escaped = false;
            index += 1;
            continue;
        }
        match byte {
            b'\\' => escaped = true,
            b'[' if !in_character_class => in_character_class = true,
            b']' if in_character_class => in_character_class = false,
            b'(' if !in_character_class
                && bytes.get(index + 1) == Some(&b'?')
                && (matches!(bytes.get(index + 2), Some(b'=') | Some(b'!'))
                    || matches!(
                        (bytes.get(index + 2), bytes.get(index + 3)),
                        (Some(b'<'), Some(b'=' | b'!'))
                    )) =>
            {
                return true;
            }
            _ => {}
        }
        index += 1;
    }
    false
}

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

    #[test]
    fn rewrites_exact_resend_zod_email_pattern_without_weakening_validation() {
        let schema = json!({
            "type": "object",
            "properties": {
                "email": {"type": "string", "pattern": "^(?!\\.)(?!.*\\.\\.)([A-Za-z0-9_'+\\-\\.]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"}
            }
        });

        let sanitized = sanitize_openai_tool_schema(&schema);
        let pattern = sanitized["properties"]["email"]["pattern"]
            .as_str()
            .unwrap();
        assert_eq!(
            pattern,
            r"^[A-Za-z0-9_'+\-](?:[A-Za-z0-9_'+\-]|\.[A-Za-z0-9_'+\-])*@([A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$"
        );

        let email = Regex::new(pattern).unwrap();
        for valid in [
            "a@example.com",
            "first.last+tag@example.co.uk",
            "o'neill@example.com",
            "_@example.com",
        ] {
            assert!(email.is_match(valid), "expected valid: {valid}");
        }
        for invalid in [
            ".first@example.com",
            "first..last@example.com",
            "first.@example.com",
            "missing-at.example.com",
            "first@example",
        ] {
            assert!(!email.is_match(invalid), "expected invalid: {invalid}");
        }
    }

    #[test]
    fn sanitizer_preserves_literal_data_while_visiting_schema_branches() {
        let schema = json!({
            "type":"object",
            "properties":{"payload":{
                "type":"object",
                "default":{"pattern":"(?=literal)"},
                "enum":[{"pattern":"(?=literal)"}],
                "examples":[{"pattern":"(?=literal)"}]
            }},
            "$defs":{"code":{"type":"string","pattern":"(?=real)"}},
            "anyOf":[{"properties":{"code":{"type":"string","pattern":"(?<=real)"}}}]
        });
        let mut expected = schema.clone();
        expected["$defs"]["code"] = json!({"type":"string","description":"Validation for this value is enforced by the tool."});
        expected["anyOf"][0]["properties"]["code"] = json!({"type":"string","description":"Validation for this value is enforced by the tool."});
        assert_eq!(sanitize_openai_tool_schema(&schema), expected);
    }

    #[test]
    fn leaves_supported_patterns_and_unrelated_schema_unchanged() {
        let schema = json!({
            "type": "object",
            "properties": {
                "slug": {"type": "string", "pattern": "^[a-z0-9-]+$"},
                "literal": {"type": "string", "pattern": "^\\\\(\\\\?=value$"},
                "class": {"type": "string", "pattern": "[(?=]+"}
            }
        });

        assert_eq!(sanitize_openai_tool_schema(&schema), schema);
    }

    #[test]
    fn removes_other_unsupported_lookaround_but_preserves_server_validation_notice() {
        for pattern in ["^(?=.{3,20}$)[a-z]+$", "a(?!b)", "(?<=a)b", "(?<!a)b"] {
            for description in [None, Some("Tenant-scoped identifier.")] {
                let mut item = json!({"type":"string","pattern":pattern});
                if let Some(description) = description {
                    item["description"] = json!(description);
                }
                let schema = json!({"type":"array","items":item});
                let notice = match description {
                    Some(_) => {
                        "Tenant-scoped identifier. Validation for this value is enforced by the tool."
                    }
                    None => "Validation for this value is enforced by the tool.",
                };
                assert_eq!(
                    sanitize_openai_tool_schema(&schema),
                    json!({"type":"array","items":{"type":"string","description":notice}})
                );
            }
        }
    }
}

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

    #[test]
    fn strict_schema_closes_nested_objects_and_makes_optional_fields_nullable() {
        let normalized = strict_openai_tool_schema(&json!({
            "type": "object",
            "properties": {
                "fixed": {"type": "string"},
                "nested": {
                    "type": "object",
                    "properties": {"value": {"type": "integer"}},
                    "required": ["value"]
                },
                "list": {"type": "array", "items": {
                    "type": "object", "properties": {"flag": {"type": "boolean"}}
                }}
            },
            "required": ["fixed"]
        }))
        .unwrap();

        assert_eq!(
            normalized,
            json!({
                "type":"object", "additionalProperties":false, "required":["fixed","list","nested"],
                "properties": {
                    "fixed":{"type":"string"},
                    "nested":{"type":["object","null"],"properties":{"value":{"type":"integer"}},"required":["value"],"additionalProperties":false},
                    "list":{"type":["array","null"],"items":{
                        "type":"object","properties":{"flag":{"type":["boolean","null"]}},"required":["flag"],"additionalProperties":false
                    }}
                }
            })
        );
    }

    #[test]
    fn strict_schema_declines_ambiguous_composition() {
        for input in [
            json!({"type":"array","items":{"type":"string"}}),
            json!({"type":"object","required":["missing"]}),
            json!({"type":"object","properties":{"value":{"type":"array"}}}),
            json!({"type":"object","properties":{"value":{"type":["object","string"]}}}),
            json!({"type":"object","properties":{"value":{"anyOf":[]}}}),
        ] {
            assert_eq!(strict_openai_tool_schema(&input), None, "{input}");
        }
        assert!(
            strict_openai_tool_schema(&json!({
                "type": "object",
                "allOf": [{"type": "object", "properties": {"value": {"type": "string"}}}]
            }))
            .is_none()
        );
        assert!(
            strict_openai_tool_schema(&json!({
                "type": "object",
                "properties": {"value": {"type": "string", "pattern": "^[a-z]+$"}}
            }))
            .is_none()
        );
    }
    #[test]
    fn optional_enum_allows_null_without_relaxing_required_enum() {
        let input = json!({"type":"object", "properties": {
            "choice":{"type":"string","enum":["a","b"]},
            "fixed":{"type":"string","enum":["x"]},
            "nullable":{"type":["string","null"],"enum":["a",null]}
        }, "required":["fixed"]});
        assert_eq!(
            strict_openai_tool_schema(&input),
            Some(json!({
                "type":"object", "properties": {
                    "choice":{"type":["string","null"],"enum":["a","b",null]},
                    "fixed":{"type":"string","enum":["x"]},
                    "nullable":{"type":["string","null"],"enum":["a",null]}
                }, "required":["choice","fixed","nullable"],"additionalProperties":false
            }))
        );
    }

    #[test]
    fn strict_schema_declines_dictionary_shapes_instead_of_discarding_entries() {
        for additional in [json!(true), json!({"type":"string"})] {
            let input = json!({"type":"object","properties":{"labels":{
                "type":"object","additionalProperties":additional
            }},"required":["labels"]});
            assert_eq!(strict_openai_tool_schema(&input), None, "{input}");
        }
    }
}