Skip to content

Commit 8d0d792

Browse files
author
Dhamivibez
committed
fix(groq): handle empty object schemas for Groq structured output
Ensure object schemas always have a properties field and remove required when there are no properties, as Groq rejects empty required arrays
1 parent 8108a70 commit 8d0d792

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

packages/typescript/ai-groq/src/tools/function-tool.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export function convertFunctionToolToAdapterFormat(tool: Tool): FunctionTool {
2020
required: [],
2121
}) as JSONSchema
2222

23+
// Ensure object schemas always have properties (e.g. z.object({}) may produce { type: 'object' } without properties)
24+
if (inputSchema.type === 'object' && !inputSchema.properties) {
25+
inputSchema.properties = {}
26+
}
27+
2328
const jsonSchema = makeGroqStructuredOutputCompatible(
2429
inputSchema,
2530
inputSchema.required || [],

packages/typescript/ai-groq/src/utils/schema-converter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ export function makeGroqStructuredOutputCompatible(
8787
}
8888

8989
result.properties = properties
90+
// Groq rejects `required` when there are no properties, even if it's an empty array
91+
if (allPropertyNames.length > 0) {
92+
result.required = allPropertyNames
93+
} else {
94+
delete result.required
95+
}
9096
result.required = allPropertyNames
9197
result.additionalProperties = false
9298
}

0 commit comments

Comments
 (0)