Skip to content

[Responses API] Structured output #1586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 2, 2025

Conversation

Wauplin
Copy link
Contributor

@Wauplin Wauplin commented Jul 2, 2025

Built on top of #1576.

Based on https://platform.openai.com/docs/guides/structured-outputs

Works both with and without streaming.

Non-stream

Run

pnpm run example structured_output

(which core logic is:)

(...)
const response = await openai.responses.parse({
	model: "Qwen/Qwen2.5-VL-72B-Instruct",
	provider: "nebius",
	input: [
		{
			role: "system",
			content: "You are a helpful math tutor. Guide the user through the solution step by step.",
		},
		{ role: "user", content: "how can I solve 8x + 7 = -23" },
	],
	text: {
		format: zodTextFormat(MathReasoning, "math_reasoning"),
	},
});
(...)

Output:

{
  steps: [
    {
      explanation: 'To solve for x, we need to isolate it on one side of the equation. We start by subtracting 7 from both sides of the equation.',
      output: '8x + 7 - 7 = -23 - 7'
    },
    {
      explanation: 'Simplify the equation after performing the subtraction.',
      output: '8x = -30'
    },
    {
      explanation: 'Now that we have isolated the term with x, we divide both sides by 8 to get x by itself.',
      output: '8x / 8 = -30 / 8'
    },
    {
      explanation: 'Perform the division to find the value of x.',
      output: 'x = -30 / 8'
    },
    {
      explanation: 'Simplify the fraction if possible.',
      output: 'x = -15 / 4'
    }
  ],
  final_answer: 'The solution is x = -15/4 or x = -3.75.'
}

Stream

Run

pnpm run example structured_output_streaming

(which core logic is:)

const stream = openai.responses
	.stream({
		model: "Qwen/Qwen2.5-VL-72B-Instruct",
		provider: "nebius",
		instructions: "Extract the event information.",
		input: "Alice and Bob are going to a science fair on Friday.",
		text: {
			format: zodTextFormat(CalendarEvent, "calendar_event"),
		},
	})
	.on("response.refusal.delta", (event) => {
		process.stdout.write(event.delta);
	})
	.on("response.output_text.delta", (event) => {
		process.stdout.write(event.delta);
	})
	.on("response.output_text.done", () => {
		process.stdout.write("\n");
	})
	.on("response.error", (event) => {
		console.error(event.error);
	});

const result = await stream.finalResponse();
console.log(result.output_parsed);

Output:

{
  "name": "Science Fair",
  "date": "Friday",
  "participants": ["Alice", "Bob"]
}
{
  name: 'Science Fair',
  date: 'Friday',
  participants: [ 'Alice', 'Bob' ]
}

@Wauplin Wauplin merged commit 9c06344 into responses-server Jul 2, 2025
5 checks passed
@Wauplin Wauplin deleted the responses-server-structured-output branch July 2, 2025 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant