ooai_llm.profiles

Serializable chat-model profiles and runtime wrapper.

Purpose:

Provide a durable Pydantic profile that can be checked into configuration and used to create LangChain chat models through the existing factory surface. The LLM runtime owns one profile, lazily builds the runnable, and records observed usage/cost metadata after calls.

Attributes

Classes

CacheKeyPolicy

Serializable cache policy for a chat-model profile.

ChatModelProfileResolution

Resolved model/profile metadata without constructing a chat model.

ChatModelProfile

Serializable configuration for creating a LangChain chat model.

LLM

Stateful runtime wrapper around a serializable ChatModelProfile.

Module Contents

ooai_llm.profiles.CacheBackendName[source]
ooai_llm.profiles.logger[source]
class ooai_llm.profiles.CacheKeyPolicy(/, **data: Any)[source]

Bases: pydantic.BaseModel

Serializable cache policy for a chat-model profile.

model_config[source]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

enabled: bool = True[source]
namespace: str = 'default'[source]
key: str | None = None[source]
backend: CacheBackendName | None = None[source]
class ooai_llm.profiles.ChatModelProfileResolution(/, **data: Any)[source]

Bases: pydantic.BaseModel

Resolved model/profile metadata without constructing a chat model.

model_config[source]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model: ooai_llm.types.ModelString[source]
profile_id: str | None = None[source]
metadata: ooai_llm.metadata.ModelInfo[source]
reasoning: ooai_llm.reasoning.ReasoningResolution | None = None[source]
cache_namespace: str | None = None[source]
cache_key: str | None = None[source]
class ooai_llm.profiles.ChatModelProfile(/, **data: Any)[source]

Bases: pydantic.BaseModel

Serializable configuration for creating a LangChain chat model.

model_config[source]

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

id: str | None = None[source]
model: str | None = None[source]
alias: ooai_llm.settings.ModelAliasName | None = None[source]
provider: str | ooai_llm.providers.Provider | None = None[source]
preset: ooai_llm.settings.ModelPresetName = 'default'[source]
temperature: float | None = None[source]
max_tokens: int | None = None[source]
top_p: float | None = None[source]
frequency_penalty: float | None = None[source]
presence_penalty: float | None = None[source]
seed: int | None = None[source]
stop: str | list[str] | None = None[source]
max_retries: int | None = None[source]
parallel_tool_calls: bool | None = None[source]
timeout: float | None = None[source]
streaming: bool | None = None[source]
configurable_fields: str | list[str] | None = None[source]
config_prefix: str | None = None[source]
model_kwargs: dict[str, Any] = None[source]
constructor_kwargs: dict[str, Any] = None[source]
reasoning: ooai_llm.reasoning.ReasoningInput = None[source]
auto_refresh_models: bool | None = None[source]
force_model_refresh: bool = False[source]
billing_model_name: str | None = None[source]
cache: CacheKeyPolicy | bool | None = None[source]
tags: list[str] = None[source]
metadata: dict[str, Any] = None[source]
run_name: str | None = None[source]
cost_labels: dict[str, str] = None[source]
to_json(*, indent: int = 2) str[source]

Serialize the profile to deterministic JSON.

classmethod from_json(text: str) ChatModelProfile[source]

Deserialize a profile from JSON text.

create_llm(*, settings: ooai_llm.settings.AppSettings | None = None, **kwargs: Any) Any[source]

Create a LangChain chat model from this profile.

create_bundle(*, settings: ooai_llm.settings.AppSettings | None = None, messages: Any = None, tools: collections.abc.Sequence[Any] | None = None, **kwargs: Any) ooai_llm.metadata.CreatedLLMBundle[source]

Create a chat model plus resolved metadata from this profile.

create_runtime(*, settings: ooai_llm.settings.AppSettings | None = None, recorder: ooai_llm.callbacks.UsageRecorder | None = None, budget: ooai_llm.callbacks.BudgetPolicy | None = None, id: str | None = None, uuid: str | uuid.UUID | None = None) LLM[source]

Create an LLM runtime from this profile.

create_dspy_lm(*, settings: ooai_llm.settings.AppSettings | None = None, **kwargs: Any) Any[source]

Create a DSPy LM from this profile.

DSPy is an optional dependency and is imported lazily. Install ooai-llm[dspy] before using this helper.

resolve(*, settings: ooai_llm.settings.AppSettings | None = None, auto_refresh_models: bool | None = None, force_model_refresh: bool | None = None) ChatModelProfileResolution[source]

Resolve model, metadata, reasoning, and cache key material.

cache_key() str[source]

Return the explicit or stable derived cache key for this profile.

class ooai_llm.profiles.LLM(*, profile: ChatModelProfile | collections.abc.Mapping[str, Any], settings: ooai_llm.settings.AppSettings | None = None, recorder: ooai_llm.callbacks.UsageRecorder | None = None, budget: ooai_llm.callbacks.BudgetPolicy | None = None, id: str | None = None, uuid: str | uuid.UUID | None = None)[source]

Stateful runtime wrapper around a serializable ChatModelProfile.

profile[source]
settings[source]
recorder[source]
budget = None[source]
uuid[source]
id[source]
property bundle: ooai_llm.metadata.CreatedLLMBundle[source]

Return the lazily built bundle.

property runnable: Any[source]

Return the lazily built LangChain runnable.

property model: ooai_llm.types.ModelString[source]

Return the resolved model string.

property metadata: ooai_llm.metadata.ModelInfo[source]

Return resolved model metadata.

property usage_events: list[ooai_llm.callbacks.UsageEvent][source]

Return recorded usage events.

property total_tokens: int[source]

Return total observed tokens.

property total_cost_usd: decimal.Decimal[source]

Return total observed/estimated cost.

property usage_summary: Any[source]

Return grouped observed usage totals.

set_profile(profile: ChatModelProfile | collections.abc.Mapping[str, Any], *, id: str | None = None) None[source]

Replace the profile and invalidate the cached runnable.

invalidate() None[source]

Drop the cached runnable and bundle.

refresh(*, force: bool = False) ooai_llm.metadata.CreatedLLMBundle[source]

Rebuild the runnable, optionally forcing model-default refresh.

create_dspy_lm(**kwargs: Any) Any[source]

Create a DSPy LM from this runtime’s profile and settings.

The returned object is a native DSPy LM. The richer program/runnable layer belongs in ooai-agents.

invoke(input: Any, config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any) Any[source]

Invoke the underlying chat model and record observed usage.

async ainvoke(input: Any, config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any) Any[source]

Async invoke the underlying chat model and record observed usage.

batch(inputs: collections.abc.Sequence[Any], config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any) list[Any][source]

Batch invoke the underlying chat model and record observed usage.

async abatch(inputs: collections.abc.Sequence[Any], config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any) list[Any][source]

Async batch invoke the underlying chat model and record observed usage.

stream(input: Any, config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any)[source]

Stream from the underlying chat model.

async astream(input: Any, config: collections.abc.Mapping[str, Any] | None = None, **kwargs: Any)[source]

Async stream from the underlying chat model.