Skip to main content

TypedPrompt

Trait TypedPrompt 

Source
pub trait TypedPrompt: WasmCompatSend + WasmCompatSync {
    type TypedRequest<'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a>: IntoFuture<Output = Result<T, StructuredOutputError>>
       where Self: 'a;

    // Required method
    fn prompt_typed<T>(
        &self,
        prompt: impl Into<Message> + WasmCompatSend,
    ) -> Self::TypedRequest<'_, T>
       where T: JsonSchema + DeserializeOwned + WasmCompatSend;
}
Expand description

Trait defining a high-level typed prompt interface for structured output.

This trait provides an ergonomic way to get typed responses from an LLM by automatically generating a JSON schema from the target type and deserializing the response.

§Example

use rig::prelude::*;
use schemars::JsonSchema;
use serde::Deserialize;

#[derive(Debug, Deserialize, JsonSchema)]
struct WeatherForecast {
    city: String,
    temperature_f: f64,
    conditions: String,
}

let agent = client.agent("gpt-4o").build();
let forecast: WeatherForecast = agent
    .prompt_typed("What's the weather in NYC?")
    .await?;

Required Associated Types§

Source

type TypedRequest<'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a>: IntoFuture<Output = Result<T, StructuredOutputError>> where Self: 'a

The type of the typed prompt request returned by prompt_typed.

Required Methods§

Source

fn prompt_typed<T>( &self, prompt: impl Into<Message> + WasmCompatSend, ) -> Self::TypedRequest<'_, T>

Send a prompt and receive a typed structured response.

The JSON schema for T is automatically generated and sent to the provider. Providers that support native structured outputs will constrain the model’s response to match this schema.

§Type Parameters
  • T - The target type to deserialize the response into. Must implement JsonSchema (for schema generation), DeserializeOwned (for deserialization), and WasmCompatSend (for async compatibility).
§Example
// Type can be inferred
let forecast: WeatherForecast = agent.prompt_typed("What's the weather?").await?;

// Or specified explicitly with turbofish
let forecast = agent.prompt_typed::<WeatherForecast>("What's the weather?").await?;

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<M, P> TypedPrompt for &Agent<M, P>
where M: CompletionModel, P: PromptHook<M> + 'static,

Source§

type TypedRequest<'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a> = TypedPromptRequest<'a, T, Standard, M, P> where &Agent<M, P>: 'a

Source§

impl<M, P> TypedPrompt for Agent<M, P>
where M: CompletionModel, P: PromptHook<M> + 'static,

Source§

type TypedRequest<'a, T: JsonSchema + DeserializeOwned + WasmCompatSend + 'a> = TypedPromptRequest<'a, T, Standard, M, P> where Agent<M, P>: 'a