API Reference¶
load_agent¶
load_agent
¶
load_agent(
path: str | Path,
*,
model_override: str | None = None,
logfire_token: str | None = None,
) -> TextAgent[Any]
Load a TextAgent from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the agent definition file (.txt with TOML front-matter) |
required |
model_override
|
str | None
|
Override the model specified in the file |
None
|
logfire_token
|
str | None
|
Logfire token for auto-instrumentation (or set LOGFIRE_TOKEN env var) |
None
|
Returns:
| Type | Description |
|---|---|
TextAgent[Any]
|
A configured TextAgent ready to run. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file doesn't exist. |
AgentDefinitionError
|
If the agent definition is invalid. |
Example
Source code in src/textagents/loader.py
import textagents
# Basic
agent = textagents.load_agent("agent.txt")
# With model override
agent = textagents.load_agent("agent.txt", model_override="openai:gpt-4o-mini")
# With Logfire
agent = textagents.load_agent("agent.txt", logfire_token="...")
TextAgent¶
TextAgent
¶
Bases: Generic[OutputT]
A text-defined PydanticAI agent wrapper.
Provides a simplified interface for running agents defined in .txt files with TOML front-matter. Handles template interpolation, input validation, and forwards to the underlying PydanticAI Agent.
Example
text_agent = load_agent("safety_judge.txt")
# Async (primary)
result = await text_agent.run(user_input="Hello", model_output="Hi there!")
print(result.reasoning)
print(result.no_hate) # True
# Sync convenience
result = text_agent.run_sync(user_input="Hello", model_output="Hi there!")
# Advanced: access underlying PydanticAI agent
full_result = await text_agent.agent.run("custom prompt")
print(full_result.all_messages())
Attributes:
| Name | Type | Description |
|---|---|---|
spec |
AgentSpec
|
The parsed agent specification |
output_model |
type[OutputT]
|
The dynamically generated Pydantic model for outputs |
agent |
Agent[None, OutputT]
|
The underlying PydanticAI agent (public for advanced usage) |
Initialize TextAgent.
Use load_agent() instead of constructing directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
AgentSpec
|
Parsed agent specification |
required |
output_model
|
type[OutputT]
|
Generated Pydantic output model |
required |
agent
|
Agent[None, OutputT]
|
Configured PydanticAI agent |
required |
Source code in src/textagents/agent.py
run
async
¶
Run the agent asynchronously with the given inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**inputs
|
Any
|
Named inputs matching agent.input_type definitions. Use @filepath syntax to load from file. |
{}
|
Returns:
| Type | Description |
|---|---|
OutputT
|
The validated output model instance (result.output from PydanticAI). |
Raises:
| Type | Description |
|---|---|
MissingInputError
|
If required inputs are not provided. |
InputTypeError
|
If input types cannot be coerced. |
Source code in src/textagents/agent.py
run_sync
¶
Run the agent synchronously (convenience wrapper).
Equivalent to asyncio.run(self.run(**inputs)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**inputs
|
Any
|
Named inputs matching agent.input_type definitions. |
{}
|
Returns:
| Type | Description |
|---|---|
OutputT
|
The validated output model instance. |
Source code in src/textagents/agent.py
# Async
result = await agent.run(text="Hello")
# Sync
result = agent.run_sync(text="Hello")
# Properties
agent.name # Agent name
agent.model # Model string
agent.required_inputs # List of required input names
Results¶
Results are Pydantic model instances: