mirror of
https://github.com/livekit/agents.git
synced 2026-12-06 04:08:35 +00:00
38 lines
1.0 KiB
Python
38 lines
1.0 KiB
Python
from typing import Literal
|
|
|
|
import pytest
|
|
from jsonschema import Draft202012Validator, ValidationError
|
|
from pydantic import BaseModel
|
|
|
|
from livekit.agents.llm.utils import to_openai_response_format
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
class StringResponse(BaseModel):
|
|
value: Literal["known"] | None
|
|
|
|
|
|
class IntegerResponse(BaseModel):
|
|
value: Literal[1] | None
|
|
|
|
|
|
class BooleanResponse(BaseModel):
|
|
value: Literal[True] | None
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"response_type, value",
|
|
[(StringResponse, "known"), (IntegerResponse, 1), (BooleanResponse, True)],
|
|
)
|
|
def test_nullable_literal_response_schema_accepts_null_and_the_literal(
|
|
response_type: type[BaseModel], value: object
|
|
) -> None:
|
|
wire_schema = to_openai_response_format(response_type)["json_schema"]["schema"]
|
|
validator = Draft202012Validator(wire_schema)
|
|
for payload in ({"value": None}, {"value": value}):
|
|
response_type.model_validate(payload)
|
|
validator.validate(payload)
|
|
with pytest.raises(ValidationError):
|
|
validator.validate({"value": "invalid"})
|