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:
@@ -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 };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user