---
hide_table_of_contents: true
---

import CodeBlock from "@theme/CodeBlock";

# Structured Output with OpenAI functions

:::tip Compatibility
Must be used with an [OpenAI functions](https://platform.openai.com/docs/guides/gpt/function-calling) model.
:::

This example shows how to leverage OpenAI functions to output objects that match a given format for any given input.
It converts input schema into an OpenAI function, then forces OpenAI to call that function to return a response in the correct format.

You can use it where you would use a chain with a [`StructuredOutputParser`](/docs/modules/model_io/output_parsers), but it doesn't require any special
instructions stuffed into the prompt. It will also more reliably output structured results with higher `temperature` values, making it better suited
for more creative applications.

**Note:** The outermost layer of the input schema must be an object.

## Usage

Though you can pass in JSON Schema directly, you can also define your output schema using the popular [Zod](https://zod.dev) schema
library and convert it with the `zod-to-json-schema` package. To do so, install the following packages:

```bash npm2yarn
npm install zod zod-to-json-schema
```

### Format Text into Structured Data

import FormatExample from "@examples/chains/openai_functions_structured_format.ts";

<CodeBlock language="typescript">{FormatExample}</CodeBlock>

### Generate a Database Record

Though we suggest the above [Expression Language example](/docs/expression_language/cookbook), here's an example
of using the `createStructuredOutputChainFromZod` convenience method to return a classic LLMChain:

import GenerateExample from "@examples/chains/openai_functions_structured_generate.ts";

<CodeBlock language="typescript">{GenerateExample}</CodeBlock>
