Skip to content

Commit 5a648e9

Browse files
committed
support chat completions json_object with tool call
for anthropic and openai-format models that do not natively support structured output.
1 parent 6418a0a commit 5a648e9

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

packages/proxy/src/proxy.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,6 +1713,7 @@ async function fetchOpenAI(
17131713
delete bodyData.response_format;
17141714
break;
17151715
case "json_schema":
1716+
case "json_object":
17161717
if (
17171718
bodyData.model.startsWith("gpt") ||
17181719
bodyData.model.startsWith("o1") ||
@@ -1734,8 +1735,20 @@ async function fetchOpenAI(
17341735
function: {
17351736
name: "json",
17361737
description: "Output the result in JSON format",
1737-
parameters: responseFormatParsed.data.json_schema.schema,
1738-
strict: responseFormatParsed.data.json_schema.strict,
1738+
parameters:
1739+
responseFormatParsed.data.type === "json_schema"
1740+
? responseFormatParsed.data.json_schema.schema
1741+
: {
1742+
type: "object",
1743+
description:
1744+
"A flexible schema that accepts any JSON object.",
1745+
properties: {},
1746+
additionalProperties: true,
1747+
},
1748+
strict:
1749+
responseFormatParsed.data.type === "json_schema"
1750+
? responseFormatParsed.data.json_schema.strict
1751+
: false,
17391752
},
17401753
},
17411754
];
@@ -2113,7 +2126,10 @@ async function fetchAnthropicChatCompletions({
21132126

21142127
let isStructuredOutput = false;
21152128
const parsed = responseFormatSchema.safeParse(oaiParams.response_format);
2116-
if (parsed.success && parsed.data.type === "json_schema") {
2129+
if (
2130+
parsed.success &&
2131+
(parsed.data.type === "json_schema" || parsed.data.type === "json_object")
2132+
) {
21172133
isStructuredOutput = true;
21182134
if (params.tools || params.tool_choice) {
21192135
throw new ProxyBadRequestError(
@@ -2124,7 +2140,15 @@ async function fetchAnthropicChatCompletions({
21242140
{
21252141
name: "json",
21262142
description: "Output the result in JSON format",
2127-
input_schema: parsed.data.json_schema.schema,
2143+
input_schema:
2144+
parsed.data.type === "json_schema"
2145+
? parsed.data.json_schema.schema
2146+
: {
2147+
type: "object",
2148+
description: "A flexible schema that accepts any JSON object.",
2149+
properties: {},
2150+
additionalProperties: true,
2151+
},
21282152
},
21292153
];
21302154
params.tool_choice = { type: "tool", name: "json" };

0 commit comments

Comments
 (0)