yera.models.context.llm
Module containing the LLM context infrastructure.
Consists of
- the context var that contains the current llm context.
- the llm context class itself.
- the llm_context function gets the active llm context.
Symbols
EmptyResponse
RuntimeErrorEmptyStructResponseA stream produced no response tokens.
has_active_llm
has_active_llm() → boolCheck whether there is an active llm.
Returns
true if there's an llm false if not
llm_context
llm_context() → LLMContextGet the current active LLM context.
Returns
the active context.
LLMContext
BaseModelContextManages the currently-active LLM instance and its execution context.
An LLM context encapsulates the interface to an LLM provider and provides access to the app workspace where message history and variables are stored. It acts as a context manager for proper initialization and cleanup of the LLM interface during execution.
Attributes: interface: The LLM provider interface for sending prompts and receiving responses. app_meta: Metadata about the app context this LLM is executing within.
Methods
LLMContext.add_sys_line
add_sys_line(
content: str,
) → NoneAdd a system message to the workspace and emit an event.
Parameters
The system message content.
LLMContext.add_user_line
add_user_line(
content: str,
) → NoneAdd a user message to the workspace and emit an event.
Parameters
The user message content.
LLMContext.add_assistant_line
add_assistant_line(
content: str,
thinking: str | None = None,
on_wire: bool = True,
provider_data: list[dict[str, object]] | None = None,
) → NoneAdd an assistant message to the workspace.
Parameters
The assistant message content.
The assistant's thinking trace content (optional).
whether the assistant line should be included in the model context.
Opaque provider state required for later requests.
LLMContext.add_tool_call_line
add_tool_call_line(
content: str,
tool_id: str,
call_id: str,
tool_schema: dict,
provider_data: list[dict[str, object]] | None = None,
) → NoneRecord a tool call message in the workspace.
Parameters
JSON string containing the tool arguments.
Name of the invoked tool.
Unique identifier for this invocation.
JSON schema describing the tool input.
Opaque provider state required for later requests.
LLMContext.add_tool_result_line
add_tool_result_line(
content: str,
tool_id: str,
call_id: str,
tool_schema: dict,
) → NoneRecord a tool result message in the workspace.
Parameters
JSON string of the tool's result/output.
Name of the invoked tool (model class name).
Unique ID matching the original tool call.
Full JSON schema used for validation (same as in add_tool_call_line).
LLMContext.insert
insert(
content: str,
) → NoneInsert string content as a user message.
LLMContext.gen
gen(
instruction: str | None = None,
on_wire: bool = True,
**kwargs,
) → strGenerate an LLM prose response (not structured gen).
Parameters
additional instruction to condition the LLM's generation.
whether the generated response is to be kept in the model context.
keyword args to be passed down to the LLM invocation.
Returns
the generated LLM response.
LLMContext.struct_gen
struct_gen(
instruction: str | None = None,
on_wire: bool = False,
**kwargs,
) → TStructSend a prompt to the LLM and return a structured response.
Adds the user prompt to the workspace, requests a structured response from the LLM interface, parses the JSON response, and records it in the workspace.
Parameters
The Pydantic model class to parse the structured response into.
additional instruction to condition the LLM's generation.
whether the generated response is to be kept in the model context.
Additional arguments to pass to the LLM interface.
Returns
An instance of cls populated with the LLM response data.
LLMContext.insert_result
insert_result(
result: object,
) → NoneInsert a tool result into the active conversation.
Structured values are converted to transport-safe JSON using Yera's serialization infrastructure. String results remain plain text so they can be inserted without JSON quoting.
Parameters
Tool result to add to the model context.
Raises
If the result contains a value unsupported by Yera's typing infrastructure.
If a supported value cannot be serialized.
LLMContext.__enter__
__enter__()Enter the context manager and initialize the LLM interface.
Captures the current app metadata, sets up the workspace, and initializes the LLM interface.
Returns
This context instance.
LLMContext.__exit__
__exit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
)Exit the context manager and clean up the LLM interface.
Parameters
The exception type if an error occurred, else None.
The exception value if an error occurred, else None.
The exception traceback if an error occurred, else None.