# ModelBehaviorError: Invalid output type

**URL:** <https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443>\
**Category:** API\
**Tags:** api, agents-sdk\
**Created:** [June 23, 2025, 2:42pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443 "2025-06-23T14:42:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [June 23, 2025, 2:42pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/1 "2025-06-23T14:42:45Z")

</div>

Hello everyone. I use OpenAI Agents SDK but I have a problem. Even if I define an outputType, model rarely gives outputs don’t align with the ouputType defined. This causes my backend service to go down because of an unhandled exception.

```auto
export const Answer = catchAsync(async (req: CustomRequestChatbot, res: Response) => {
  
...
...
...
 const runner = new Runner({
      workflowName: 'mas_storefront',
      groupId: chatSession.sessionId,
      traceId: trace_${message.id},
      tracingDisabled: false
    })

    const result = await runner.run(
      startAgent,
      message.message.content,
      {
        context: context,
        previousResponseId: chatSession.lastResponseId,
        stream: true,
      },

    );
    
    for await (const event of result.toTextStream()) {
      res.write(event);
    }

    res.end();
...
...
...
}

```

```auto
Unhandled rejection ModelBehaviorError: Invalid output type
    at executeToolsAndSideEffects (/app/node_modules/@openai/agents-core/src/runImplementation.ts:614:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Runner.#runStreamLoop (/app/node_modules/@openai/agents-core/src/run.ts:781:30) {

```

catchAsync doesn’t catch the error. I also tried to wrap .run() and .toTextStream() blocks with try/catch before, but it also didin’t work. I couldn’t find a way to handle this error. My backend is crushing every time API returns a response in the wrong format.

Also is it normal for API to give out of format response occasionally?

---

<div class="post-metadata">

**Author:** ![sergeliatko](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sergeliatko/32/816_2.png) [@sergeliatko](https://community.openai.com/u/sergeliatko)\
**Post date:** [June 23, 2025, 4:38pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/2 "2025-06-23T16:38:23Z")

</div>

Hi, what is the format it must return?  
Is your error intermittent or stable on specific formats?  
Is it the same model that must provide this format?

Thanks.

---

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [June 23, 2025, 7:21pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/3 "2025-06-23T19:21:09Z")

</div>

> [@sergeliatko](#):
>
> what is the format it must return?  
> Is your error intermittent or stable on specific formats?  
> Is it the same model that must provide this format?
> 
> Thanks.

Actually the format is always the same and defined by zod object as OpenAI Agents TS SDK suggests. However, sometimes the response is a string which causes the error.

```auto
export const AgentsResponseStorefront = z.object({
    content: z.string(),
    suggestive_answers: z.array(z.string()),
    recommended_products: z.array(z.string()),
    forms: z.array(z.object({
        type: z.string(),
        fields: z.array(z.object({
            name: z.string(),
            type: z.string(),
        }))
    }))
})

```

---

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [June 23, 2025, 8:44pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/4 "2025-06-23T20:44:12Z")

</div>

See [GPT4.1 doesn't follow strict json schema](https://community.openai.com/t/gpt4-1-doesnt-follow-strict-json-schema/1296679#)

---

<div class="post-metadata">

**Author:** ![sergeliatko](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sergeliatko/32/816_2.png) [@sergeliatko](https://community.openai.com/u/sergeliatko)\
**Post date:** [June 23, 2025, 8:53pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/5 "2025-06-23T20:53:09Z")

</div>

Do you guys use field description in the schemas? I do because it helps focus the model on the exact values.

Also for the schema in the example looks like open door for hallucinations if not detailed in the system prompt (too loose as definition).

But having no other info makes me guess more than help.

---

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [June 23, 2025, 9:11pm UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/6 "2025-06-23T21:11:45Z")

</div>

I don’t think it is possible to give descriptions in zod schema. Their descriptions are in the system prompt. However, this is not important because it should give an output following the schema type. Descriptions may only effect the content’s relevance.

---

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [June 24, 2025, 7:19am UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/7 "2025-06-24T07:19:17Z")

</div>

There are two key points of this issue:

- OpenAI API (GPT 4.1 in particular) sometimes provides unstructured output while a structure for the output is defined
- [GitHub - openai/openai-agents-js: A lightweight, powerful framework for multi-agent workflows and voice agents](https://github.com/openai/openai-agents-js) has a bug that makes it impossible to catch `ModelBehaviorError: Invalid output type` error emitted by `@openai/agents-core/src/runImplementation.ts`.

The first one is a big issue and is being discussed here: [GPT4.1 doesn't follow strict json schema](https://community.openai.com/t/gpt4-1-doesnt-follow-strict-json-schema/1296679)

I created an issue for the second one and it is being fixed by OpenAI guys right now:

> <https://github.com/openai/openai-agents-js/issues/139#issuecomment-2998999137>
>
> When using OpenAI’s models with the Agents library, I sometimes receive outputs …that don’t conform to the JSON schema (validated by Zod) I provided. This doesn’t seem to be an issue with the library itself, so I suspect I might be overlooking something—structured outputs were always correct before I started using Agents.
> 
> I can share trace IDs from example conversations if that would help troubleshoot.

---

<div class="post-metadata">

**Author:** ![sergeliatko](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/sergeliatko/32/816_2.png) [@sergeliatko](https://community.openai.com/u/sergeliatko)\
**Post date:** [June 24, 2025, 9:17am UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/8 "2025-06-24T09:17:08Z")

</div>

Well, in this case, the bulletproof technique I used before the JSON schemas were supported by llms would be reconstruct each field separately in parallel calls and then use regular code to build the json response.

---

<div class="post-metadata">

**Author:** ![mdabdulhalimrafi](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@mdabdulhalimrafi](https://community.openai.com/u/mdabdulhalimrafi)\
**Post date:** [July 1, 2025, 7:43am UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/9 "2025-07-01T07:43:08Z")

</div>

I also had the exact same error with the Agents library. The way I got it to work was, instead of using the Zod object schema, I replaced it with the raw schema definition. Then it worked!

I assume it could be a compatibility issue with Zod sometimes.

---

<div class="post-metadata">

**Author:** ![baki.kucukcakiroglu](https://sea2.discourse-cdn.com/openai1/user_avatar/community.openai.com/baki.kucukcakiroglu/32/618324_2.png) [@baki.kucukcakiroglu](https://community.openai.com/u/baki.kucukcakiroglu)\
**Post date:** [July 16, 2025, 9:33am UTC](https://community.openai.com/t/modelbehaviorerror-invalid-output-type/1296443/10 "2025-07-16T09:33:45Z")

</div>

> [GitHub - openai/openai-agents-js: A lightweight, powerful framework for multi-agent workflows and voice agents](https://github.com/openai/openai-agents-js) has a bug that makes it impossible to catch `ModelBehaviorError: Invalid output type` error emitted by `@openai/agents-core/src/runImplementation.ts`.

This point of the issue was resolved by @seratch in [Fix a bug where some of the exceptions thrown from runImplementation.ts could be unhandled by seratch · Pull Request #148 · openai/openai-agents-js · GitHub](https://github.com/openai/openai-agents-js/pull/148)

`OpenAI API (GPT 4.1 in particular) sometimes provides unstructured output while a structure for the output is defined` is still valid and should be discussed under [GPT4.1 doesn't follow strict json schema](https://community.openai.com/t/gpt4-1-doesnt-follow-strict-json-schema/1296679)
