Last active
March 9, 2025 08:46
-
-
Save IvsonEmidio/ab81bef7f4b849de7171683d956e3e2c to your computer and use it in GitHub Desktop.
Fix Groq JSON Validation error 400
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# BEFORE | |
const response = await groqClient.chat.completions.create({ | |
messages: [ | |
// Your messages here | |
], | |
model: "your-model", | |
response_format: { | |
type: "json_object" | |
} | |
}); | |
// Extract content by parsing the JSON string | |
const result = JSON.parse(response.choices[0].message.content); | |
# After | |
const response = await groqClient.chat.completions.create({ | |
messages: [ | |
// Update your prompts to instruct the model to use the tool | |
], | |
model: "your-model", | |
tools: [{ | |
type: "function", | |
function: { | |
name: "yourToolName", | |
description: "Description of what the tool does", | |
parameters: { | |
type: "object", | |
properties: { | |
// Define the expected JSON structure here | |
your_field: { | |
type: "string", | |
description: "Description of this field" | |
} | |
}, | |
required: ["your_field"] | |
} | |
} | |
}], | |
tool_choice: { | |
type: "function", | |
function: { | |
name: "yourToolName" | |
} | |
} | |
}); | |
// Extract content from the tool call | |
const toolCall = response.choices[0].message.tool_calls[0]; | |
const result = JSON.parse(toolCall.function.arguments); | |
!GROQ ARE A SHAME TO NOT FIXING THIS SHIT! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment