🏠 Home

OpenAI integration error - 400 Invalid schema for response_format

Recently, I’ve encountered an error while integrating with OpenAI API and langchain.

400 Invalid schema for response_format ‘extract’: schema must be a JSON Schema of ‘type: “object”’, got ‘type: “array”’.

I was using structured output with zod schema. There are quite some challenges with both Gemini and OpenAI. I have encountered this specifically from using OpenAI API. And I want to share how I was able to resolve this issue.

Here’s a example of how I used langchain.

const schema = z.object({
    // your schema definition
});

const llm = new ChatOpenAI();
const model = llm.withStructuredOutput(schema);
const output = await model.invoke(input)
  1. Make sure the root of the schema is an object.
  2. If using an object still gives you an error of 400 Invalid schema, look into transforming zod schema to JSON schema like the following. This is just transforming the zod schema object to a JSON format of the validation definition.
z.toJSONSchema(schema, { target: 'draft-7' })

Note, we are assuming you are using zod v4.