Skip to main content
Set response_format to a Pydantic model, and Curator asks the model for output that matches it. Your parse method then gets an instance of that model instead of a string.

Generate poems with structured output

This is what the code does.
  1. The Pydantic models Poem and Poems describe the output you want from the LLM.
  2. Poet sets response_format = Poems. You can also pass response_format=Poems when you create the object.
  3. For each topic, prompt builds the prompt and Curator sends it to the LLM.
  4. parse gets a Poems object and returns one row for each poem, with the topic next to it.
The result has one poem per row, and each row keeps the topic it came from.

Chain LLM calls

You can pass the output of one LLM object to the next. In this example, a Muse generates the topics and the Poet from above writes poems about them.
muse() has no input, so Curator sends one request. You can pass the CuratorResponse it returns straight to poet. Each step fans out the rows of the step before it, so a short chain can produce a large dataset.
Not every model supports structured output. If a model does not support it, Curator raises an error before it sends any requests.