umpirix/sdk/python/tests/typing/pydantic_response_models.py
caelum0x 4847e35451 OpenJev: open-source typed decision service
Typed decision/classification service with an in-process gte-small ONNX
embedding engine and lexical fallback, JS/Python SDKs, adapters, and the
otsone model family. Apache-2.0 (sub-packages keep their MIT licenses).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 00:12:36 +03:00

52 lines
2.8 KiB
Python

from typing import TypeVar
from pydantic import BaseModel
from typing_extensions import assert_type
from tests.test_pydantic_response_models import KnownResponse, TypedSystemOneResponse
from openjev import AsyncTypeSafeClient, ChoiceAnswer, Noul, NoulAnswer, ScoreAnswer, SystemOneResponse, TypeSafeClient
PydanticT = TypeVar("PydanticT", bound=BaseModel)
def sync(client: TypeSafeClient, response_model: type[KnownResponse] | None) -> None:
questions = {"spam": Noul()}
assert_type(client.system_one("x", questions), SystemOneResponse)
assert_type(client.system_one("x", questions, response_model=None), SystemOneResponse)
assert_type(client.system_one("x", questions, response_model=SystemOneResponse), SystemOneResponse)
assert_type(client.system_one("x", questions, response_model=response_model), KnownResponse | SystemOneResponse)
standalone = client.system_one("x", questions, response_model=KnownResponse)
assert_type(standalone, KnownResponse)
assert_type(standalone.answers.spam, NoulAnswer)
typed = client.system_one("x", {"spam": Noul()}, response_model=TypedSystemOneResponse)
assert_type(typed, TypedSystemOneResponse)
assert_type(typed.spam, NoulAnswer)
assert_type(typed.nouls, dict[str, NoulAnswer])
assert_type(typed.choices, dict[str, ChoiceAnswer])
assert_type(typed.scores, dict[str, ScoreAnswer])
async def asynchronous(client: AsyncTypeSafeClient, response_model: type[KnownResponse] | None) -> None:
questions = {"spam": Noul()}
assert_type(await client.system_one("x", questions), SystemOneResponse)
assert_type(await client.system_one("x", questions, response_model=None), SystemOneResponse)
assert_type(await client.system_one("x", questions, response_model=SystemOneResponse), SystemOneResponse)
assert_type(await client.system_one("x", questions, response_model=response_model), KnownResponse | SystemOneResponse)
standalone = await client.system_one("x", questions, response_model=KnownResponse)
assert_type(standalone, KnownResponse)
assert_type(standalone.answers.spam, NoulAnswer)
typed = await client.system_one("x", {"spam": Noul()}, response_model=TypedSystemOneResponse)
assert_type(typed, TypedSystemOneResponse)
assert_type(typed.spam, NoulAnswer)
assert_type(typed.nouls, dict[str, NoulAnswer])
assert_type(typed.choices, dict[str, ChoiceAnswer])
assert_type(typed.scores, dict[str, ScoreAnswer])
def generic_sync(client: TypeSafeClient, response_model: type[PydanticT]) -> PydanticT:
return client.system_one("x", {"spam": Noul()}, response_model=response_model)
async def generic_async(client: AsyncTypeSafeClient, response_model: type[PydanticT]) -> PydanticT:
return await client.system_one("x", {"spam": Noul()}, response_model=response_model)