Task Class

A decorated resilient task function. Not callable directly.

Use run (invoke-and-wait), start (fire-and-forget), or <xref:azure.ai.agentserver.core.tasks.Task.options> (per-call overrides).

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 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="")
   ````
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.

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
str

Unique task identifier.

Default value: None
input
<xref:Input>

Typed input value.

input_id
str | None

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 (payload["last_input_id"]).

Two modes:

  • Idempotency-only (input_id set, if_last_input_id unset): advances the stored chain head unconditionally. Always succeeds; no precondition check. Use this when chain ordering is enforced by another mechanism (e.g. task_id collapse + TaskConflictError / steering-queue sequencing for conversation-grouped multi-turn).

  • Chain-extension (paired with if_last_input_id): implements HTTP If-Match-style optimistic concurrency on the input queue — see if_last_input_id below.

Default value: None
if_last_input_id
str | None

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 If-Match: <etag> semantics. Requires input_id to also be supplied (raises TypeError otherwise — a precondition without an advancing id is not meaningful).

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_last_input_id precondition does not match the stored last input id.

If if_last_input_id is supplied without input_id

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
str

Unique task identifier.

Default value: None
input
<xref:Input>

Typed input value.

input_id
str | None

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 (payload["last_input_id"]).

Two modes:

  • Idempotency-only (input_id set, if_last_input_id unset): advances the stored chain head unconditionally. Always succeeds; no precondition check. Use this when chain ordering is enforced by another mechanism (e.g. task_id collapse + TaskConflictError / steering-queue sequencing for conversation-grouped multi-turn).

  • Chain-extension (paired with if_last_input_id): implements HTTP If-Match-style optimistic concurrency on the input queue — see if_last_input_id below.

Default value: None
if_last_input_id
str | None

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 If-Match: <etag> semantics. Requires input_id to also be supplied (raises TypeError otherwise — a precondition without an advancing id is not meaningful).

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_last_input_id precondition does not match the stored last input id.

If if_last_input_id is supplied without input_id

Attributes

name