fix: move thinkingConfig to root level of Gemini request body

thinkingConfig must be a top-level key in the request, NOT nested inside
generationConfig. This is required for gemini-2.5-flash and newer models.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro
2026-07-14 20:24:17 +00:00
co-authored by Claude Sonnet 4.6
parent a311a7f0d3
commit eb6bb8e7f3
3 changed files with 30 additions and 26 deletions
@@ -107,10 +107,10 @@ class ShowConfiguracionChat extends Component
->withHeaders(['Content-Type' => 'application/json'])
->post("{$url}?key={$key}", [
'contents' => [['parts' => [['text' => 'Responde solo "ok"']]]],
'generationConfig' => array_merge(
['maxOutputTokens' => 5, 'temperature' => 0],
preg_match('/gemini-(2\.5|3[\.\d]*)/', $model) ? ['thinkingConfig' => ['thinkingBudget' => 0]] : []
),
'generationConfig' => ['maxOutputTokens' => 5, 'temperature' => 0],
...(preg_match('/gemini-(2\.5|3[\.\d]*)/', $model)
? ['thinkingConfig' => ['thinkingBudget' => 0]]
: []),
]);
$ms = (int) ((microtime(true) - $inicio) * 1000);
+11 -9
View File
@@ -21,14 +21,17 @@ class GeminiIntentService
$this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent";
}
private function generationConfig(array $extra = []): array
private function buildRequest(array $contents, array $genConfig = []): array
{
$config = array_merge(['temperature' => 0.1, 'maxOutputTokens' => 100], $extra);
// 2.5+ models require thinkingConfig to avoid response format issues
$body = [
'contents' => $contents,
'generationConfig' => array_merge(['temperature' => 0.1, 'maxOutputTokens' => 100], $genConfig),
];
// thinkingConfig goes at root level (NOT inside generationConfig) for 2.5+ models
if (preg_match('/gemini-(2\.5|3[\.\d]*)/', $this->model)) {
$config['thinkingConfig'] = ['thinkingBudget' => 0];
$body['thinkingConfig'] = ['thinkingBudget' => 0];
}
return $config;
return $body;
}
/**
@@ -47,10 +50,9 @@ class GeminiIntentService
try {
$response = Http::withHeaders(['Content-Type' => 'application/json'])
->timeout(8)
->post("{$this->apiUrl}?key={$this->apiKey}", [
'contents' => [['parts' => [['text' => $prompt]]]],
'generationConfig' => $this->generationConfig(),
]);
->post("{$this->apiUrl}?key={$this->apiKey}",
$this->buildRequest([['parts' => [['text' => $prompt]]]])
);
$tiempoMs = (int) ((microtime(true) - $inicio) * 1000);
+15 -13
View File
@@ -21,13 +21,16 @@ class GeminiVisionService
$this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent";
}
private function generationConfig(array $extra = []): array
private function buildRequest(array $contents, array $genConfig = []): array
{
$config = array_merge(['temperature' => 0.1, 'maxOutputTokens' => 200], $extra);
$body = [
'contents' => $contents,
'generationConfig' => array_merge(['temperature' => 0.1, 'maxOutputTokens' => 200], $genConfig),
];
if (preg_match('/gemini-(2\.5|3[\.\d]*)/', $this->model)) {
$config['thinkingConfig'] = ['thinkingBudget' => 0];
$body['thinkingConfig'] = ['thinkingBudget' => 0];
}
return $config;
return $body;
}
/**
@@ -48,17 +51,16 @@ class GeminiVisionService
$inicio = microtime(true);
try {
$contents = [[
'parts' => [
['text' => $prompt],
['inline_data' => ['mime_type' => $mimeType, 'data' => $base64]],
],
]];
$response = Http::withHeaders(['Content-Type' => 'application/json'])
->timeout(15)
->post("{$this->apiUrl}?key={$this->apiKey}", [
'contents' => [[
'parts' => [
['text' => $prompt],
['inline_data' => ['mime_type' => $mimeType, 'data' => $base64]],
],
]],
'generationConfig' => $this->generationConfig(),
]);
->post("{$this->apiUrl}?key={$this->apiKey}", $this->buildRequest($contents));
$tiempoMs = (int) ((microtime(true) - $inicio) * 1000);