structured
Class info¶
Classes¶
Name | Children | Inherits |
---|---|---|
BaseResponseDefinition llmling_agent.models.result_types Base class for response definitions. |
||
MessageNode llmling_agent.messaging.messagenode Base class for all message processing nodes. |
||
StructuredAgent llmling_agent.agent.structured Wrapper for Agent that enforces a specific result type. |
🛈 DocStrings¶
LLMling integration with PydanticAI for AI-powered resource interaction.
StructuredAgent
¶
Bases: MessageNode[TDeps, TResult]
Wrapper for Agent that enforces a specific result type.
This wrapper ensures the agent always returns results of the specified type. The type can be provided as: - A Python type for validation - A response definition name from the manifest - A complete response definition instance
Source code in src/llmling_agent/agent/structured.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
|
__aenter__
async
¶
__aenter__() -> Self
Enter async context and set up MCP servers.
Called when agent enters its async context. Sets up any configured MCP servers and their tools.
Source code in src/llmling_agent/agent/structured.py
115 116 117 118 119 120 121 122 |
|
__aexit__
async
¶
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
)
Exit async context.
Source code in src/llmling_agent/agent/structured.py
124 125 126 127 128 129 130 131 |
|
__init__
¶
__init__(
agent: Agent[TDeps] | StructuredAgent[TDeps, TResult] | Callable[..., TResult],
result_type: type[TResult] | str | ResponseDefinition,
*,
tool_name: str | None = None,
tool_description: str | None = None,
)
Initialize structured agent wrapper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent[TDeps] | StructuredAgent[TDeps, TResult] | Callable[..., TResult]
|
Base agent to wrap |
required |
result_type
|
type[TResult] | str | ResponseDefinition
|
Expected result type: - BaseModel / dataclasses - Name of response definition in manifest - Complete response definition instance |
required |
tool_name
|
str | None
|
Optional override for tool name |
None
|
tool_description
|
str | None
|
Optional override for tool description |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If named response type not found in manifest |
Source code in src/llmling_agent/agent/structured.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
_run
async
¶
_run(
*prompt: AnyPromptType | TResult,
result_type: type[TResult] | None = None,
model: ModelType = None,
tool_choice: bool | str | list[str] = True,
store_history: bool = True,
message_id: str | None = None,
conversation_id: str | None = None,
wait_for_connections: bool | None = None,
) -> ChatMessage[TResult]
Run with fixed result type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prompt
|
AnyPromptType | TResult
|
Any prompt-compatible object or structured objects of type TResult |
()
|
result_type
|
type[TResult] | None
|
Expected result type: - BaseModel / dataclasses - Name of response definition in manifest - Complete response definition instance |
None
|
model
|
ModelType
|
Optional model override |
None
|
tool_choice
|
bool | str | list[str]
|
Control tool usage: - True: Allow all tools - False: No tools - str: Use specific tool - list[str]: Allow specific tools |
True
|
store_history
|
bool
|
Whether the message exchange should be added to the context window |
True
|
message_id
|
str | None
|
Optional message id for the returned message. Automatically generated if not provided. |
None
|
conversation_id
|
str | None
|
Optional conversation id for the returned message. |
None
|
wait_for_connections
|
bool | None
|
Whether to wait for all connections to complete |
None
|
Source code in src/llmling_agent/agent/structured.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
from_callback
classmethod
¶
from_callback(
callback: ProcessorCallback[TResult], *, name: str | None = None, **kwargs: Any
) -> StructuredAgent[None, TResult]
Create a structured agent from a processing callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
callback
|
ProcessorCallback[TResult]
|
Function to process messages. Can be: - sync or async - with or without context - with explicit return type |
required |
name
|
str | None
|
Optional name for the agent |
None
|
**kwargs
|
Any
|
Additional arguments for agent |
{}
|
Example
class AnalysisResult(BaseModel):
sentiment: float
topics: list[str]
def analyze(msg: str) -> AnalysisResult:
return AnalysisResult(sentiment=0.8, topics=["tech"])
analyzer = StructuredAgent.from_callback(analyze)
Source code in src/llmling_agent/agent/structured.py
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
is_busy
¶
is_busy() -> bool
Check if agent is currently processing tasks.
Source code in src/llmling_agent/agent/structured.py
396 397 398 |
|
run_iter
async
¶
run_iter(
*prompt_groups: Sequence[AnyPromptType | Image | PathLike[str]], **kwargs: Any
) -> AsyncIterator[ChatMessage[Any]]
Forward run_iter to wrapped agent.
Source code in src/llmling_agent/agent/structured.py
271 272 273 274 275 276 277 278 |
|
run_job
async
¶
run_job(
job: Job[TDeps, TResult],
*,
store_history: bool = True,
include_agent_tools: bool = True,
) -> ChatMessage[TResult]
Execute a pre-defined job ensuring type compatibility.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
job
|
Job[TDeps, TResult]
|
Job configuration to execute |
required |
store_history
|
bool
|
Whether to add job execution to conversation history |
True
|
include_agent_tools
|
bool
|
Whether to include agent's tools alongside job tools |
True
|
Returns:
Type | Description |
---|---|
ChatMessage[TResult]
|
Task execution result |
Raises:
Type | Description |
---|---|
JobError
|
If job execution fails or types don't match |
ValueError
|
If job configuration is invalid |
Source code in src/llmling_agent/agent/structured.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
|
validate_against
async
¶
Check if agent's response satisfies stricter criteria.
Source code in src/llmling_agent/agent/structured.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|