Task Class
Constructor
Task(fn: Callable[[TaskContext[Input]], Awaitable[Output]], opts: TaskOptions, input_type: type[Input], output_type: type[Output])
Parameters
| Name | Description |
|---|---|
|
fn
Required
|
The decorated async function. |
|
opts
Required
|
Frozen task options. |
|
input_type
Required
|
Extracted input type. |
|
output_type
Required
|
Extracted output type. |
Methods
| get_active_run |
Return a TaskRun handle for an active (in-progress) task. : consults the store, not only
in-memory state. If the record is in-progress with a dead
lease, performs inline reclaim as a hidden side effect and
returns a usable TaskRun bound to the new lifetime.
Terminal records return Enables late-join consumers to iterate a running task's stream
without being the original caller of param task_id: The task identifier. type task_id: str return:
A TaskRun bound to the active task's stream handler,
or rtype: TaskRun[Output] | None Example:
|
| run |
Run a lifecycle-aware resilient task and return the result. Automatically starts, resumes, or recovers the task based on its current state:
Note title, tags, retry, are configured on the @task(...) decorator (or via <xref:Task.options>), not per-call. This is enforced so the values survive crash recovery: after the container crashes and the framework re-enters the task, it has only the registered decorator's options to work with — a per-call override would silently disappear at the crash boundary. Session identity is platform-derived from the FOUNDRY_AGENT_SESSION_ID environment variable. |
| start |
Start a lifecycle-aware resilient task and return a handle. Follows the same lifecycle rules as run but returns immediately with a TaskRun handle instead of blocking. Note title, tags, retry, are configured on the @task(...) decorator (or via <xref:Task.options>), not per-call — see run for the rationale. Session identity is platform-derived from the FOUNDRY_AGENT_SESSION_ID environment variable. |
get_active_run
Return a TaskRun handle for an active (in-progress) task.
: consults the store, not only
in-memory state. If the record is in-progress with a dead
lease, performs inline reclaim as a hidden side effect and
returns a usable TaskRun bound to the new lifetime.
Terminal records return None. Eviction returns None.
Enables late-join consumers to iterate a running task's stream
without being the original caller of start()/run(),
AND covers the orphan-resurrection case where the previous
lifetime crashed without notice.
param task_id: The task identifier.
type task_id: str
return:
A TaskRun bound to the active task's stream handler,
or None if not active / terminal / evicted.
rtype: TaskRun[Output] | None
Example:
# In another coroutine or request handler:
run = await my_task.get_active_run("task-123")
if run is not None:
async for chunk in run:
print(chunk, end="")
````
async get_active_run(task_id: str) -> TaskRun[Output] | None
Parameters
| Name | Description |
|---|---|
|
task_id
Required
|
|
run
Run a lifecycle-aware resilient task and return the result.
Automatically starts, resumes, or recovers the task based on its current state:
No task / pending → create and start (
entry_mode="fresh")Suspended → resume with new input (
entry_mode="resumed")In-progress (stale) → recover (
entry_mode="recovered")In-progress (not stale) → raise TaskConflictError
Completed → raise TaskConflictError
Note
title, tags, retry, are
configured on the @task(...)
decorator (or via <xref:Task.options>), not per-call. This
is enforced so the values survive crash recovery: after the
container crashes and the framework re-enters the task, it
has only the registered decorator's options to work with — a
per-call override would silently disappear at the crash
boundary. Session identity is platform-derived from the
FOUNDRY_AGENT_SESSION_ID environment variable.
async run(*, task_id: str | None = None, input: Input, input_id: str | None = None, if_last_input_id: str | None = None) -> Output
Keyword-Only Parameters
| Name | Description |
|---|---|
|
task_id
|
Unique task identifier. Default value: None
|
|
input
|
<xref:Input>
Typed input value. |
|
input_id
|
Optional identifier for the input being accepted. When
supplied, the framework records it as the task's most-recently-accepted
input id in a framework-reserved slot ( Two modes:
Default value: None
|
|
if_last_input_id
|
Optional precondition. When supplied, the framework
verifies that the task's currently-stored last input id equals this value
before accepting the new input. If the precondition does not hold (a
concurrent caller advanced the queue, or the caller's view is stale),
raises LastInputIdPreconditionFailed before any state mutation.
Modelled on HTTP Default value: None
|
Returns
| Type | Description |
|---|---|
|
<xref:azure.ai.agentserver.core.tasks.TaskResult>[<xref:Output>]
|
The task result wrapper with output, status, and suspension info. |
Exceptions
| Type | Description |
|---|---|
|
On unhandled exception. |
|
|
If the task is already in-progress or completed. |
|
|
If
the |
|
|
If |
start
Start a lifecycle-aware resilient task and return a handle.
Follows the same lifecycle rules as run but returns immediately with a TaskRun handle instead of blocking.
Note
title, tags, retry, are
configured on the @task(...)
decorator (or via <xref:Task.options>), not per-call —
see run for the rationale. Session identity is
platform-derived from the FOUNDRY_AGENT_SESSION_ID
environment variable.
async start(*, task_id: str | None = None, input: Input, input_id: str | None = None, if_last_input_id: str | None = None) -> TaskRun[Output]
Keyword-Only Parameters
| Name | Description |
|---|---|
|
task_id
|
Unique task identifier. Default value: None
|
|
input
|
<xref:Input>
Typed input value. |
|
input_id
|
Optional identifier for the input being accepted. When
supplied, the framework records it as the task's most-recently-accepted
input id in a framework-reserved slot ( Two modes:
Default value: None
|
|
if_last_input_id
|
Optional precondition. When supplied, the framework
verifies that the task's currently-stored last input id equals this value
before accepting the new input. If the precondition does not hold (a
concurrent caller advanced the queue, or the caller's view is stale),
raises LastInputIdPreconditionFailed before any state mutation.
Modelled on HTTP Default value: None
|
Returns
| Type | Description |
|---|---|
|
TaskRun[<xref:Output>]
|
A handle to the running task. |
Exceptions
| Type | Description |
|---|---|
|
If the task is already in-progress or completed. |
|
|
If
the |
|
|
If |