M
Mastra•2w ago
Mehul

Structured output breaks for gemini flash 2.5

I get the following error Function calling with a response mime type: 'application/json' is unsupported I don't get this error for flash 2.0. When using mastra agents.
14 Replies
arpitBhalla
arpitBhalla•2w ago
happening to me as well
Mehul
MehulOP•2w ago
doesn't happen on the vercel ai sdk, confirmed this
arpitBhalla
arpitBhalla•2w ago
Yes, it wasn't happening few days back, but I recently upgraded mastra@beta
Mastra Triager
Mastra Triager•2w ago
šŸ“ Created GitHub issue: https://github.com/mastra-ai/mastra/issues/10666 šŸ” If you're experiencing an error, please provide a minimal reproducible example whenever possible to help us resolve it quickly. šŸ™ Thank you for helping us improve Mastra!
Abhi Aiyer
Abhi Aiyer•2w ago
import { generateObject } from "ai";
import { Agent } from "@mastra/core/agent";
import { google } from "@ai-sdk/google";
import z from "zod";

const agent = new Agent({
id: 'weather-agent',
name: 'Weather Agent',
instructions: "You are a helpful weather assistant that provides accurate weather information and can help planning activities based on the weather.",
model: google('gemini-2.5-flash'),
});

const res1 = await agent.generate([
{
role: 'user',
content: 'What is the capital of France?',
},
],
{
structuredOutput: {
schema: z.object({
capital: z.string(),
}),
}
});

console.log(res1.object);

const result = await generateObject({
model: google('gemini-2.5-flash'),
prompt: "What is the capital of France?",
schema: z.object({
capital: z.string(),
}),
});

console.log(result.object);
import { generateObject } from "ai";
import { Agent } from "@mastra/core/agent";
import { google } from "@ai-sdk/google";
import z from "zod";

const agent = new Agent({
id: 'weather-agent',
name: 'Weather Agent',
instructions: "You are a helpful weather assistant that provides accurate weather information and can help planning activities based on the weather.",
model: google('gemini-2.5-flash'),
});

const res1 = await agent.generate([
{
role: 'user',
content: 'What is the capital of France?',
},
],
{
structuredOutput: {
schema: z.object({
capital: z.string(),
}),
}
});

console.log(res1.object);

const result = await generateObject({
model: google('gemini-2.5-flash'),
prompt: "What is the capital of France?",
schema: z.object({
capital: z.string(),
}),
});

console.log(result.object);
is it when a tool call is happening? The above does not throw
arpitBhalla
arpitBhalla•2w ago
for me, yes, i was trying with a tool @Abhi Aiyer
Mehul
MehulOP•7d ago
I thought it was to do with structure output, but it happens to me when I use memory
memory: new Memory({
storage: storage,
options: {
workingMemory: {
enabled: true,
}
}
}),
memory: new Memory({
storage: storage,
options: {
workingMemory: {
enabled: true,
}
}
}),
Abhi Aiyer
Abhi Aiyer•7d ago
We'll take a look and see whats up!
Daniel Lew
Daniel Lew•6d ago
@Mehul you mentioned this error doesn't happen with ai sdk? Can you show me how you're doing that? I seem to get the same error in ai sdk and this is because it's an issue with the model API itself see: https://discuss.ai.google.dev/t/function-calling-with-a-response-mime-type-application-json-is-unsupported/105093 There are ways we can possible handle this, but just want to make sure that is a broader issue first
Google AI Developers Forum
Function calling with a response mime type: 'application/json' is u...
Root Cause The Gemini API does not currently support structured tool calling / function calling with JSON outputs (unlike OpenAI models). The API only supports returning text or multimodal responses, not strongly typed JSON function responses when using tool calling. Confirmed via these discussions: firebase/genkit issue #703 deprecated-genera...
arpitBhalla
arpitBhalla•6d ago
Could you share how to handle it without json?
Daniel Lew
Daniel Lew•6d ago
To bypass this you can set jsonPromptInjection to true and it will work it won't use the native response_format This is an option in the structuredOutput property
Mehul
MehulOP•6d ago
I get the error only with the memory tool call, my initial assumption of structured data was wrong
Daniel Lew
Daniel Lew•6d ago
google gemini (only 2.5) doesn't natively support pass tools along with structured outputs. It's a limitation of the gemini API that we have ways to get around it using jsonPromptInjection instead of the native response format. Semantic recall and working memory both use tools to work, so those tools are passed to the agent, so even if you don't have any tools you're manually passing in, those count as tools I'm adding a callout in our docs for this issue https://github.com/mastra-ai/mastra/pull/10719, We're going to redo the structured output section soon to make it have better discoverability. For now we'll leave it at that and if the documentation doesn't solve this then we'll build some magic into the framework to automatically add that option for gemini models. But we do want to leave that as the last resort as it's better for the user to be aware something is happening and have to opt in
arpitBhalla
arpitBhalla•6d ago
Thanks, Although I was able to use it with semantic recall, but adding a tool was causing the issue.

Did you find this page helpful?