feat: Add OpenAI API response_format support for structured JSON output

Translates OpenAI response_format parameter into Claude-compatible system
prompt instructions, enabling structured JSON output for json_schema and
json_object types.

Co-authored-by: Nick Roth <nlr06886@gmail.com>
Made-with: Cursor
This commit is contained in:
Nick Roth
2026-03-12 16:30:47 +07:00
committed by decolua
parent b0c6b61398
commit 75270ea755
2 changed files with 139 additions and 0 deletions

View File

@@ -100,6 +100,21 @@ export function openaiToClaudeRequest(model, body, stream) {
}
}
// Handle response_format for JSON mode
if (body.response_format) {
const responseFormat = body.response_format;
if (responseFormat.type === "json_schema" && responseFormat.json_schema?.schema) {
const schemaJson = JSON.stringify(responseFormat.json_schema.schema, null, 2);
systemParts.push(`You must respond with valid JSON that strictly follows this JSON schema:
\`\`\`json
${schemaJson}
\`\`\`
Respond ONLY with the JSON object, no other text.`);
} else if (responseFormat.type === "json_object") {
systemParts.push("You must respond with valid JSON. Respond ONLY with a JSON object, no other text.");
}
}
// System with Claude Code prompt and cache_control
const claudeCodePrompt = { type: "text", text: CLAUDE_SYSTEM_PROMPT };