Skip to main content

Module structured

Module structured 

Source
Expand description

Structured output — typed LLM responses with schema validation.

This module provides high-level functions that combine schema derivation, LLM generation, validation, and deserialization into a single call.

§Non-streaming

generate_object sends a request with a JSON Schema constraint and returns a fully validated, deserialized T:

use llm_stack_core::structured::{generate_object, GenerateObjectConfig};
use llm_stack_core::{ChatMessage, ChatParams};
use serde::Deserialize;

#[derive(Deserialize, schemars::JsonSchema)]
struct Person {
    name: String,
    age: u32,
}

let params = ChatParams {
    messages: vec![ChatMessage::user("Generate a person named Alice aged 30")],
    ..Default::default()
};

let result = generate_object::<Person>(provider, params, GenerateObjectConfig::default()).await?;
assert_eq!(result.value.name, "Alice");

§Streaming

stream_object_async yields PartialObject<T> events as JSON tokens arrive. Each event carries the accumulated JSON so far, and the final event includes the fully deserialized object.

Structs§

GenerateObjectConfig
Configuration for generate_object and stream_object_async.
GenerateObjectResult
The result of a successful generate_object call.
PartialObject
A partially-received structured object from a streaming response.

Functions§

collect_stream_object
Collects a ChatStream into a PartialObject<T>.
generate_object
Generates a typed object from the LLM with schema validation.
stream_object_async
Async streaming variant of generate_object.