xr_ai_models#
Unified service protocols and OpenAI-compatible clients for XR AI models.
Repository code talks to the typed *Service protocols. The concrete
OpenAICompat* clients cover every in-tree backend and external
OpenAI-compatible endpoints.
Submodules#
Attributes#
A supported text, image, or video part in a chat message. |
|
Image bytes, a filesystem path, a |
|
Video bytes, a filesystem path, a |
|
A model role supported by |
|
The adapter kind for OpenAI-compatible HTTP endpoints. |
|
A supported model-service adapter implementation. |
|
Any typed model-role specification stored in |
Classes#
Features supported by a configured model endpoint. |
|
One input turn in an LLM or VLM conversation. |
|
A normalized non-streaming response from a chat model. |
|
Structural interface for text-embedding services. |
|
An image reference in a multimodal chat message. |
|
Structural interface for text chat-completion services. |
|
Structural interface for speech-to-text services. |
|
Text content in a multimodal chat message. |
|
A function-tool invocation requested by a model. |
|
A function tool definition exposed to a chat model. |
|
Structural interface for text-to-speech services. |
|
A video reference in a multimodal chat message. |
|
Structural interface for visual chat-completion services. |
|
OpenAI-compatible |
|
OpenAI-compatible |
|
OpenAI-compatible |
|
OpenAI-compatible |
|
OpenAI-compatible client for image- and video-bearing chat requests. |
|
API dialect and model-specific request/response behavior. |
|
Process ownership for the endpoint that serves a model role. |
|
Configuration for a text-embedding model role. |
|
Connectivity, authentication, timeout, and readiness for an adapter. |
|
Configuration for a text chat-completion model role. |
|
Logical-name to typed model-role specifications. |
|
Configuration for a speech-to-text model role. |
|
Configuration for a text-to-speech model role. |
|
Configuration for an image- or video-language model role. |
Functions#
|
Load a YAML or JSON model profile and resolve its adapter presets. |
|
Build a model configuration from a parsed direct or |
|
Construct the embedding service for the named configuration entry. |
|
Construct the text chat service for the named configuration entry. |
|
Construct the speech-to-text service for the named configuration entry. |
|
Construct the text-to-speech service for the named configuration entry. |
|
Construct the visual chat service for the named configuration entry. |
Package Contents#
- class xr_ai_models.Capabilities#
Features supported by a configured model endpoint.
- streaming: bool = True#
Whether the endpoint supports token streaming.
- tool_calls: bool = False#
Whether the endpoint supports function-tool calls.
- vision: bool = False#
Whether the endpoint accepts image inputs.
- video: bool = False#
Whether the endpoint accepts video inputs.
- reasoning: bool = False#
Whether the endpoint can return model reasoning.
- class xr_ai_models.ChatMessage#
One input turn in an LLM or VLM conversation.
- role: Literal['system', 'user', 'assistant', 'tool']#
The participant role associated with the message.
- content: str | list[ContentPart]#
Plain text or ordered multimodal content supplied by the participant.
- tool_call_id: str | None = None#
The call identifier answered by a tool-result message, when applicable.
- class xr_ai_models.ChatResponse#
A normalized non-streaming response from a chat model.
- content: str#
The assistant’s user-visible response text.
- reasoning: str | None#
Normalized model reasoning, when the endpoint returns it.
- finish_reason: str | None#
The provider’s reason for ending generation, when supplied.
- raw: dict[str, Any]#
The unmodified provider response object.
- xr_ai_models.ContentPart#
A supported text, image, or video part in a chat message.
- class xr_ai_models.EmbeddingService#
Structural interface for text-embedding services.
- async embed(
- texts: Sequence[str],
- *,
- timeout: float | None = None,
Embed text strings, returning one vector for each input in order.
- async health() bool#
Return whether the configured endpoint is ready for requests.
- async close() None#
Release resources owned by the service.
- xr_ai_models.ImageInput#
Image bytes, a filesystem path, a
data:URL, or an HTTP(S) URL.
- class xr_ai_models.ImagePart#
An image reference in a multimodal chat message.
- url: str#
An HTTP(S) or
data:URL containing the image.
- type: Literal['image_url'] = 'image_url'#
The OpenAI-compatible discriminator for this content part.
- class xr_ai_models.LLMService#
Structural interface for text chat-completion services.
- capabilities: Capabilities#
Features supported by this service.
- async chat(
- messages: Sequence[ChatMessage],
- *,
- tools: Sequence[ToolDef] | None = None,
- max_tokens: int | None = None,
- temperature: float | None = None,
- enable_thinking: bool = False,
- thinking_budget: int | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one complete response for a sequence of chat messages.
- stream(
- messages: Sequence[ChatMessage],
- *,
- tools: Sequence[ToolDef] | None = None,
- max_tokens: int | None = None,
- temperature: float | None = None,
- enable_thinking: bool = False,
- thinking_budget: int | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream user-visible response text for a sequence of chat messages.
- async health() bool#
Return whether the configured endpoint is ready for requests.
- async close() None#
Release resources owned by the service.
- class xr_ai_models.STTService#
Structural interface for speech-to-text services.
- async transcribe(
- audio: bytes,
- *,
- sample_rate: int | None = None,
- channels: int = 1,
- timeout: float | None = None,
Transcribe WAV data or 16-bit PCM audio into text.
- async health() bool#
Return whether the configured endpoint is ready for requests.
- async close() None#
Release resources owned by the service.
- class xr_ai_models.TextPart#
Text content in a multimodal chat message.
- text: str#
The text presented to the model.
- type: Literal['text'] = 'text'#
The OpenAI-compatible discriminator for this content part.
- class xr_ai_models.ToolCall#
A function-tool invocation requested by a model.
- id: str#
The provider-assigned identifier used to submit the tool result.
- name: str#
The function name requested by the model.
- arguments: str#
JSON-encoded arguments string, per the OpenAI tool-call contract.
- class xr_ai_models.ToolDef#
A function tool definition exposed to a chat model.
- description: str#
A natural-language description of what the function does.
- parameters: dict[str, Any]#
The function parameters as a JSON Schema object.
- to_openai() dict[str, Any]#
Return this definition in OpenAI’s function-tool wire format.
- class xr_ai_models.TTSService#
Structural interface for text-to-speech services.
- async synthesize(
- text: str,
- *,
- response_format: str = 'wav',
- timeout: float | None = None,
Synthesize text and return audio in the requested format.
- async health() bool#
Return whether the configured endpoint is ready for requests.
- async close() None#
Release resources owned by the service.
- xr_ai_models.VideoInput#
Video bytes, a filesystem path, a
data:URL, or an HTTP(S) URL.
- class xr_ai_models.VideoPart#
A video reference in a multimodal chat message.
- url: str#
An HTTP(S) or
data:URL containing the video.
- type: Literal['video_url'] = 'video_url'#
The OpenAI-compatible discriminator for this content part.
- class xr_ai_models.VLMService#
Structural interface for visual chat-completion services.
- capabilities: Capabilities#
Features supported by this service.
- async ask_image(
- image: ImageInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to a question about an image.
- async ask_images(
- images: Sequence[ImageInput],
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to a question about multiple images.
- async ask_video(
- video: VideoInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to a question about a video.
- stream(
- image: ImageInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream response text for a question about an image.
- stream_images(
- images: Sequence[ImageInput],
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream response text for a question about multiple images.
- async health() bool#
Return whether the configured endpoint is ready for requests.
- async close() None#
Release resources owned by the service.
- class xr_ai_models.OpenAICompatLLM(
- base_url: str,
- model_name: str,
- *,
- capabilities: xr_ai_models.Capabilities | None = None,
- reasoning_field: str | None = None,
- default_extras: dict[str, Any] | None = None,
- api_key_env: str | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- client: httpx.AsyncClient | None = None,
OpenAI-compatible
/v1/chat/completionsclient.Used directly for plain LLMs (Llama-Nemotron, Nemotron3-Nano, Nemotron-Omni) and indirectly via
OpenAICompatVLMfor VLMs. The API key is read once fromapi_key_envduring construction. An injected HTTP client remains owned by the caller and is not closed here.- health_url#
The URL used to check endpoint readiness.
- capabilities#
Features declared for this model endpoint.
- async chat(
- messages: Sequence[xr_ai_models.ChatMessage],
- *,
- tools: Sequence[xr_ai_models.ToolDef] | None = None,
- max_tokens: int | None = None,
- temperature: float | None = None,
- enable_thinking: bool = False,
- thinking_budget: int | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate and return one normalized chat response.
Per-call generation values override endpoint defaults.
headersmay supply request context but cannot override the configured authorization header.
- async stream(
- messages: Sequence[xr_ai_models.ChatMessage],
- *,
- tools: Sequence[xr_ai_models.ToolDef] | None = None,
- max_tokens: int | None = None,
- temperature: float | None = None,
- enable_thinking: bool = False,
- thinking_budget: int | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream user-visible text deltas from a chat completion.
Malformed server-sent event lines and deltas without content are skipped. Tool-call and reasoning deltas are not yielded.
- async health() bool#
Return whether the endpoint health check succeeds.
Returns
Truewithout an HTTP request when health checks were disabled at construction time.
- async close() None#
Close the internally created HTTP client, if one is owned.
- class xr_ai_models.OpenAICompatEmbedding(
- base_url: str,
- model_name: str,
- *,
- api_key_env: str | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- client: httpx.AsyncClient | None = None,
OpenAI-compatible
/v1/embeddingsclient.The API key is read once from
api_key_envduring construction. An injected HTTP client remains owned by the caller and is not closed here.- health_url#
The URL used to check endpoint readiness.
- async embed(
- texts: Sequence[str],
- *,
- timeout: float | None = None,
Embed texts and return one vector per input in input order.
An empty input is handled locally. A response containing a different number of vectors raises
ValueError.
- async health() bool#
Return whether the endpoint health check succeeds.
Returns
Truewithout an HTTP request when health checks were disabled at construction time.
- async close() None#
Close the internally created HTTP client, if one is owned.
- class xr_ai_models.OpenAICompatSTT(
- base_url: str,
- *,
- api_key_env: str | None = None,
- timeout: float = 30.0,
- health_check: bool = True,
- client: httpx.AsyncClient | None = None,
OpenAI-compatible
/v1/audio/transcriptionsclient.The API key is read once from
api_key_envduring construction. An injected HTTP client remains owned by the caller and is not closed here.- health_url#
The URL used to check endpoint readiness.
- async transcribe(
- audio: bytes,
- *,
- sample_rate: int | None = None,
- channels: int = 1,
- timeout: float | None = None,
Transcribe WAV data or 16-bit PCM audio into text.
When
sample_rateis provided, audio is interpreted as raw signed 16-bit PCM with the given channel count and wrapped in a WAV container. Otherwise, audio must already contain a server-supported audio file.
- async health() bool#
Return whether the endpoint health check succeeds.
Returns
Truewithout an HTTP request when health checks were disabled at construction time.
- async close() None#
Close the internally created HTTP client, if one is owned.
- class xr_ai_models.OpenAICompatTTS(
- base_url: str,
- *,
- api_key_env: str | None = None,
- timeout: float = 30.0,
- health_check: bool = True,
- client: httpx.AsyncClient | None = None,
OpenAI-compatible
/v1/audio/speechclient.The API key is read once from
api_key_envduring construction. An injected HTTP client remains owned by the caller and is not closed here.- health_url#
The URL used to check endpoint readiness.
- async synthesize(
- text: str,
- *,
- response_format: str = 'wav',
- timeout: float | None = None,
Synthesize text and return audio bytes in response_format.
- async health() bool#
Return whether the endpoint health check succeeds.
Returns
Truewithout an HTTP request when health checks were disabled at construction time.
- async close() None#
Close the internally created HTTP client, if one is owned.
- class xr_ai_models.OpenAICompatVLM(
- base_url: str,
- model_name: str,
- *,
- capabilities: xr_ai_models.Capabilities | None = None,
- default_extras: dict[str, Any] | None = None,
- api_key_env: str | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- client: httpx.AsyncClient | None = None,
OpenAI-compatible client for image- and video-bearing chat requests.
Image and video paths or bytes are converted to
data:URLs before submission. An injected HTTP client remains owned by the caller and is not closed here.- property capabilities: xr_ai_models.Capabilities#
Return the features declared for the underlying chat endpoint.
- property health_url: str#
Return the URL used to check endpoint readiness.
- async ask_image(
- image: xr_ai_models.ImageInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to question about image.
- async ask_images(
- images: Sequence[xr_ai_models.ImageInput],
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to question about an ordered image sequence.
Raises
ValueErrorwhen images is empty.
- async ask_video(
- video: xr_ai_models.VideoInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Generate one response to question about video.
Raises
ValueErrorunless video support was declared in the configured capabilities.
- async stream(
- image: xr_ai_models.ImageInput,
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream response text for question about image.
- async stream_images(
- images: Sequence[xr_ai_models.ImageInput],
- question: str,
- *,
- system_prompt: str = '',
- max_tokens: int | None = None,
- temperature: float | None = None,
- timeout: float | None = None,
- headers: Mapping[str, str] | None = None,
Stream response text for a question about an ordered image sequence.
Raises
ValueErrorwhen images is empty.
- async health() bool#
Return whether the underlying chat endpoint is ready.
- async close() None#
Release resources owned by the underlying chat client.
- class xr_ai_models.AdapterSpec#
API dialect and model-specific request/response behavior.
- kind: ModelKind = 'openai_compat'#
The concrete client implementation used for this role.
- model_name: str = ''#
The model identifier sent to endpoints that require one.
- reasoning_field: str | None = None#
The response field containing reasoning, or
Nonefor auto-detection.
- capabilities: dict[str, Any]#
Overrides used to construct the service’s capability flags.
- default_extras: dict[str, Any]#
Model-specific fields merged into every request payload.
- xr_ai_models.Category#
A model role supported by
ModelsConfig.
- class xr_ai_models.DeploymentSpec#
Process ownership for the endpoint that serves a model role.
- ownership: Ownership = 'external'#
Whether the launcher starts, reuses, or does not manage the service.
- service: str | None = None#
The launcher service name for managed or reused deployments.
- class xr_ai_models.EmbeddingSpec(
- kind: ModelKind = KIND_OPENAI_COMPAT,
- base_url: str = '',
- model_name: str = '',
- api_key_env: str | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- deployment: DeploymentSpec | None = None,
- *,
- adapter: AdapterSpec | None = None,
- endpoint: EndpointSpec | None = None,
Configuration for a text-embedding model role.
- adapter: AdapterSpec#
Model-specific request and response behavior.
- endpoint: EndpointSpec#
Endpoint connectivity, authentication, and readiness settings.
- deployment: DeploymentSpec#
Launcher ownership metadata for the serving process.
- class xr_ai_models.EndpointSpec#
Connectivity, authentication, timeout, and readiness for an adapter.
- base_url: str = ''#
The endpoint root URL, without a model-specific route.
- api_key_env: str | None = None#
Environment variable containing the bearer token, when required.
- timeout: float = 60.0#
Default request timeout in seconds.
- readiness: Readiness = 'health'#
How the client determines whether the endpoint is ready.
- property health_check: bool#
Whether readiness requires a successful endpoint health check.
- xr_ai_models.KIND_OPENAI_COMPAT: ModelKind = 'openai_compat'#
The adapter kind for OpenAI-compatible HTTP endpoints.
- class xr_ai_models.LLMSpec(
- kind: ModelKind = KIND_OPENAI_COMPAT,
- base_url: str = '',
- model_name: str = '',
- api_key_env: str | None = None,
- reasoning_field: str | None = None,
- capabilities: dict[str, Any] | None = None,
- default_extras: dict[str, Any] | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- deployment: DeploymentSpec | None = None,
- *,
- adapter: AdapterSpec | None = None,
- endpoint: EndpointSpec | None = None,
Configuration for a text chat-completion model role.
- adapter: AdapterSpec#
Model-specific request and response behavior.
- endpoint: EndpointSpec#
Endpoint connectivity, authentication, and readiness settings.
- deployment: DeploymentSpec#
Launcher ownership metadata for the serving process.
- xr_ai_models.ModelKind#
A supported model-service adapter implementation.
- class xr_ai_models.ModelsConfig#
Logical-name to typed model-role specifications.
- entries: dict[str, Spec]#
Model-role specifications keyed by application-defined logical name.
- embedding(name: str) EmbeddingSpec#
Return the embedding specification named name.
- property required_credentials: tuple[str, Ellipsis]#
Return the sorted environment-variable names required by all roles.
- xr_ai_models.Spec#
Any typed model-role specification stored in
ModelsConfig.
- class xr_ai_models.STTSpec(
- kind: ModelKind = KIND_OPENAI_COMPAT,
- base_url: str = '',
- api_key_env: str | None = None,
- timeout: float = 30.0,
- health_check: bool = True,
- deployment: DeploymentSpec | None = None,
- *,
- adapter: AdapterSpec | None = None,
- endpoint: EndpointSpec | None = None,
Configuration for a speech-to-text model role.
- adapter: AdapterSpec#
Model-specific request and response behavior.
- endpoint: EndpointSpec#
Endpoint connectivity, authentication, and readiness settings.
- deployment: DeploymentSpec#
Launcher ownership metadata for the serving process.
- class xr_ai_models.TTSSpec(
- kind: ModelKind = KIND_OPENAI_COMPAT,
- base_url: str = '',
- api_key_env: str | None = None,
- timeout: float = 30.0,
- health_check: bool = True,
- deployment: DeploymentSpec | None = None,
- *,
- adapter: AdapterSpec | None = None,
- endpoint: EndpointSpec | None = None,
Configuration for a text-to-speech model role.
- adapter: AdapterSpec#
Model-specific request and response behavior.
- endpoint: EndpointSpec#
Endpoint connectivity, authentication, and readiness settings.
- deployment: DeploymentSpec#
Launcher ownership metadata for the serving process.
- class xr_ai_models.VLMSpec(
- kind: ModelKind = KIND_OPENAI_COMPAT,
- base_url: str = '',
- model_name: str = '',
- api_key_env: str | None = None,
- capabilities: dict[str, Any] | None = None,
- default_extras: dict[str, Any] | None = None,
- timeout: float = 60.0,
- health_check: bool = True,
- deployment: DeploymentSpec | None = None,
- *,
- adapter: AdapterSpec | None = None,
- endpoint: EndpointSpec | None = None,
Configuration for an image- or video-language model role.
- adapter: AdapterSpec#
Model-specific request and response behavior.
- endpoint: EndpointSpec#
Endpoint connectivity, authentication, and readiness settings.
- deployment: DeploymentSpec#
Launcher ownership metadata for the serving process.
- xr_ai_models.load_models_config(path: pathlib.Path | str) ModelsConfig#
Load a YAML or JSON model profile and resolve its adapter presets.
The file may contain model entries directly or nested below a
modelskey. Invalid entries raiseValueErrorwith the file path and role name included in the message.
- xr_ai_models.load_models_config_from_dict(
- raw: dict[str, Any],
- *,
- source: str = '<dict>',
Build a model configuration from a parsed direct or
modelsmapping.sourcelabels validation errors and is useful when the mapping did not originate from a file.
- xr_ai_models.make_embedding(
- config: xr_ai_models.ModelsConfig,
- name: str,
Construct the embedding service for the named configuration entry.
Raises
KeyErrorwhen name is absent,TypeErrorwhen it names a different model role, andValueErrorfor an unsupported adapter kind.
- xr_ai_models.make_llm(
- config: xr_ai_models.ModelsConfig,
- name: str,
Construct the text chat service for the named configuration entry.
Raises
KeyErrorwhen name is absent,TypeErrorwhen it names a different model role, andValueErrorfor an unsupported adapter kind.
- xr_ai_models.make_stt(
- config: xr_ai_models.ModelsConfig,
- name: str,
Construct the speech-to-text service for the named configuration entry.
Raises
KeyErrorwhen name is absent,TypeErrorwhen it names a different model role, andValueErrorfor an unsupported adapter kind.
- xr_ai_models.make_tts(
- config: xr_ai_models.ModelsConfig,
- name: str,
Construct the text-to-speech service for the named configuration entry.
Raises
KeyErrorwhen name is absent,TypeErrorwhen it names a different model role, andValueErrorfor an unsupported adapter kind.
- xr_ai_models.make_vlm(
- config: xr_ai_models.ModelsConfig,
- name: str,
Construct the visual chat service for the named configuration entry.
Raises
KeyErrorwhen name is absent,TypeErrorwhen it names a different model role, andValueErrorfor an unsupported adapter kind.