File size: 1,121 Bytes
03ba617
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest

from llmbench.parse import schema_errors, to_output


@pytest.mark.parametrize(

    "content",

    [

        '```json\n{"a":1}\n```',

        '<think>reasoning\nmore</think>\n{"a":1}',

        'Here is the answer: {"a":1} done',

        '<think>x</think>```json\n{"a":1}\n```',

    ],

)
def test_auto(content):
    assert to_output("auto", content) == ({"a": 1}, None)


def test_invalid_and_manual():
    output, error = to_output("auto", "bad {")
    assert output is None and error.startswith("invalid JSON:")
    assert to_output("manual", "  Привет\n ") == ({"text": "Привет"}, None)
    assert to_output("auto", "[1,2]") == ([1, 2], None)


def test_schema_validation():
    schema = {
        "type": "object",
        "required": ["a"],
        "properties": {"a": {"type": "integer"}},
    }
    assert not schema_errors({"a": 1}, schema)
    assert schema_errors({"a": "x"}, schema)
    assert not schema_errors({}, {"type": "invalid"})
    assert not schema_errors({}, "json")
    assert len(schema_errors({}, {"required": list("abcdefg")})) == 5