Skip to content

Instantly share code, notes, and snippets.

@IvsonEmidio
Last active March 9, 2025 08:46
Show Gist options
  • Save IvsonEmidio/ab81bef7f4b849de7171683d956e3e2c to your computer and use it in GitHub Desktop.
Save IvsonEmidio/ab81bef7f4b849de7171683d956e3e2c to your computer and use it in GitHub Desktop.
Fix Groq JSON Validation error 400
# 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