Skip to content

Commit 6da802d

Browse files
Merge pull request #127 from acara-app/fix/gemini-structured-output-nullable
fix: handle string 'null' from AI response in parsing methods
2 parents 16f6fdb + faf0075 commit 6da802d

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

app/Ai/Agents/HealthDataParserAgent.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function getStringValue(array $response, string $key, string $default):
174174
{
175175
$value = $response[$key] ?? null;
176176

177-
return is_string($value) ? $value : $default;
177+
return is_string($value) && $value !== 'null' ? $value : $default;
178178
}
179179

180180
/**
@@ -184,7 +184,15 @@ private function getStringOrNull(array $response, string $key): ?string
184184
{
185185
$value = $response[$key] ?? null;
186186

187-
return is_string($value) ? $value : null;
187+
if ($value === null) {
188+
return null;
189+
}
190+
191+
if (is_string($value) && $value !== 'null' && $value !== '') {
192+
return $value;
193+
}
194+
195+
return null;
188196
}
189197

190198
/**
@@ -194,6 +202,10 @@ private function getFloatOrNull(array $response, string $key): ?float
194202
{
195203
$value = $response[$key] ?? null;
196204

205+
if ($value === null || $value === 'null') {
206+
return null;
207+
}
208+
197209
return is_numeric($value) ? (float) $value : null;
198210
}
199211

@@ -204,6 +216,10 @@ private function getIntOrNull(array $response, string $key): ?int
204216
{
205217
$value = $response[$key] ?? null;
206218

219+
if ($value === null || $value === 'null') {
220+
return null;
221+
}
222+
207223
return is_numeric($value) ? (int) $value : null;
208224
}
209225
}

0 commit comments

Comments
 (0)