Skip to main content

TypedAgent

Trait TypedAgent 

Source
pub trait TypedAgent: Agent {
    // Required method
    fn run_typed_request_with_context<'life0, 'async_trait, U>(
        &'life0 mut self,
        request: RunRequest,
        context: RunContext,
    ) -> Pin<Box<dyn Future<Output = Result<TypedRunOutput<U>, AgentError>> + Send + 'async_trait>>
       where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait,
             Self: 'async_trait,
             'life0: 'async_trait;

    // Provided methods
    fn run_typed_request<'life0, 'async_trait, U>(
        &'life0 mut self,
        request: RunRequest,
    ) -> Pin<Box<dyn Future<Output = Result<TypedRunOutput<U>, AgentError>> + Send + 'async_trait>>
       where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait,
             Self: Send + 'async_trait,
             'life0: 'async_trait { ... }
    fn run_typed<'life0, 'life1, 'async_trait, U>(
        &'life0 mut self,
        input: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<U, AgentError>> + Send + 'async_trait>>
       where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait,
             Self: Send + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Optional capability: typed output (opt-in — implementations that don’t need it don’t implement it; the method doesn’t even exist at compile time).

run_typed has the same semantics as Agent::run (records input, drives the reasoning loop), but deserializes the final answer into the type parameter U once it passes validation — this run auto-generates a JSON Schema from U (schemars-derived), feeds validation failures back to the model for retry, and reports AgentError::StructuredRetriesExhausted when the budget is exhausted.

Why separate from Agent: trait generic methods are not object-safe — putting it in Agent would immediately break Box<dyn Agent> (sub-agent delegation, etc.); a separate trait leaves Box<dyn Agent> unaffected, and code with the generic bound A: TypedAgent can call it on any implementation.

No default implementation: validation retries happen inside the reasoning loop (a failure is fed back to the model and the conversation continues), while Agent::run is a one-shot call — a default implementation couldn’t retry within the loop; implementors assemble the public parts StructuredValidator (validation / feedback messages / retry budget in one) or the pure functions validate_structured / structured_retry_message inside their own loops (the built-in ReActAgent assembly is exactly this shape).

Required Methods§

Source

fn run_typed_request_with_context<'life0, 'async_trait, U>( &'life0 mut self, request: RunRequest, context: RunContext, ) -> Pin<Box<dyn Future<Output = Result<TypedRunOutput<U>, AgentError>> + Send + 'async_trait>>
where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

Typed structured run with caller-provided execution context.

Provided Methods§

Source

fn run_typed_request<'life0, 'async_trait, U>( &'life0 mut self, request: RunRequest, ) -> Pin<Box<dyn Future<Output = Result<TypedRunOutput<U>, AgentError>> + Send + 'async_trait>>
where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait, Self: Send + 'async_trait, 'life0: 'async_trait,

Typed structured run with a generated RunContext.

Source

fn run_typed<'life0, 'life1, 'async_trait, U>( &'life0 mut self, input: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<U, AgentError>> + Send + 'async_trait>>
where U: DeserializeOwned + JsonSchema + Send + Sync + 'async_trait, Self: Send + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Typed run: the final answer is deserialized into U once validation passes.

§Errors

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§