Skip to main content

generate_object

Function generate_object 

Source
pub async fn generate_object<T>(
    provider: &dyn DynProvider,
    params: ChatParams,
    config: GenerateObjectConfig,
) -> Result<GenerateObjectResult<T>, LlmError>
Expand description

Generates a typed object from the LLM with schema validation.

  1. Derives a JSON Schema from T (via schemars)
  2. Sets structured_output on ChatParams
  3. Calls the provider
  4. Parses the response text as JSON
  5. Validates against the schema
  6. Deserializes to T

Retries up to config.max_attempts times on parse/validation failures. On retry, the model’s invalid response and the validation error are appended to the message history so the model can self-correct.

§Errors

Returns LlmError if:

  • max_attempts is 0
  • The schema cannot be derived from T
  • The provider returns an error (propagated immediately, not retried)
  • All attempts fail validation (returns the last validation error)