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.
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?
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.
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.
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.
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.
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