TaskResult is the final result of an LLMAgent task. It pairs the task’s identifier with the content produced by execution, allowing the result to be matched to the original instruction. section 4.1 section 4.1.1
TaskResult is a Pydantic BaseModel with two string fields:
- task_id matches the id_ of the corresponding Task. section 4.1.1
- content holds the result of the overall task execution. section 4.1.1
Listing 4.1 Implementing Task and TaskResult
# llm_agents_from_scratch/data_structures/agent.py
import uuid
from typing import Literal
from pydantic import BaseModel, Field
class Task(BaseModel):
"""Represents a single task with an instruction.
Attributes:
id_: Identifier for task.
instruction: The instruction for the task.
"""
id_: str = Field(default_factory=lambda: str(uuid.uuid4()))
instruction: str
def __str__(self) -> str: #A
"""String representation of Task."""
return self.instruction
class TaskResult(BaseModel):
"""The result of the task execution.
Attributes:
task_id: The ID `Task` that was executed.
content: The content results of the task execution.
"""
task_id: str #B
content: str
def __str__(self) -> str:
"""String representation of TaskResult."""
return self.contentThe implementation defines Task and TaskResult together, including the identifier link and string fields. section 4.1.1
LLMAgent.run(task) returns a TaskHandler, an asyncio.Future that resolves to a TaskResult. The handler runs a processing loop in the background: get_next_step() returns either a TaskStep to execute or a TaskResult marking task completion. The loop can also stop on an error or a configured step limit. section 4.5 section 4.2.3
Retrieving a Hailstone result
For a task generating the Hailstone sequence from 4, the result can have content '4 -> 2 -> 1' and a task_id matching the submitted Task. Await the handler, or check handler.done() before calling handler.result(); calling result() too early raises InvalidStateError. The run() return value can also be awaited directly. section 4.3
In supervised execution, an operator inspects successive TaskSteps and TaskStepResults. When get_next_step() returns a TaskResult instead of another step, the operator can accept it with complete(), or reject it and pass the resulting RejectedTaskResult to the next get_next_step() call to continue processing. section 8.4.2
