Skip to main content

StructuredOutputExt

Trait StructuredOutputExt 

Source
pub trait StructuredOutputExt: BaseChatModel {
    // Provided method
    fn with_structured_output<'life0, 'life1, 'async_trait, T>(
        &'life0 self,
        schema: Value,
        prompt: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<T, StructuredOutputError>> + Send + 'async_trait>>
       where T: 'async_trait + DeserializeOwned + Serialize + Send + Sync + 'static,
             Self: Sync + 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait { ... }
}
Expand description

Trait that extends BaseChatModel with structured output capabilities.

Implementors can override the default prompt-injection strategy with provider-specific mechanisms (e.g., OpenAI function calling, Ollama JSON mode).

Provided Methods§

Source

fn with_structured_output<'life0, 'life1, 'async_trait, T>( &'life0 self, schema: Value, prompt: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<T, StructuredOutputError>> + Send + 'async_trait>>
where T: 'async_trait + DeserializeOwned + Serialize + Send + Sync + 'static, Self: Sync + 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Call the LLM with a JSON schema and prompt, returning a parsed result of type T.

The default implementation uses prompt injection: it embeds the schema into the system prompt and parses the JSON response with JsonOutputParser.

§Arguments
  • schema - A JSON Schema (serde_json::Value) describing the expected output shape.
  • prompt - The user prompt / question to send to the LLM.
§Returns

A Result<T, StructuredOutputError> where T is the deserialized output.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§

Source§

impl<M: BaseChatModel> StructuredOutputExt for M

Blanket implementation: every BaseChatModel automatically gets StructuredOutputExt.