Skip to content

Commit e0e7cfe

Browse files
committed
fix Gemini provider structured output and error detection
1 parent 08c57bd commit e0e7cfe

File tree

2 files changed

+13
-25
lines changed

2 files changed

+13
-25
lines changed

src/Providers/Gemini/HandleStream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use function rtrim;
2424
use function trim;
2525
use function is_array;
26+
use function json_encode;
2627

2728
trait HandleStream
2829
{

src/Providers/Gemini/HandleStructured.php

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use function end;
1212
use function is_array;
1313
use function json_encode;
14-
use function strtoupper;
1514

1615
trait HandleStructured
1716
{
@@ -54,37 +53,25 @@ protected function adaptSchema(array $schema): array
5453
unset($schema['additionalProperties']);
5554
}
5655

57-
if (array_key_exists('type', $schema)) {
58-
if (is_array($schema['type'])) {
59-
foreach ($schema['type'] as $type) {
60-
if ($type !== 'null') {
61-
$schema['type'] = strtoupper((string) $type);
62-
break;
63-
}
64-
}
65-
} else {
66-
$schema['type'] = strtoupper((string) $schema['type']);
56+
foreach ($schema as $key => $value) {
57+
if (is_array($value)) {
58+
$schema[$key] = $this->adaptSchema($value);
6759
}
68-
69-
$schema['type'] = match ($schema['type']) {
70-
'INT' => 'INTEGER',
71-
'BOOL' => 'BOOLEAN',
72-
'DOUBLE', 'FLOAT' => 'NUMBER',
73-
default => $schema['type']
74-
};
7560
}
7661

62+
// Always an object also if it's empty
7763
if (array_key_exists('properties', $schema) && is_array($schema['properties'])) {
78-
foreach ($schema['properties'] as $key => $value) {
79-
if (is_array($value)) {
80-
$schema['properties'][$key] = $this->adaptSchema($value);
81-
}
82-
}
8364
$schema['properties'] = (object) $schema['properties'];
8465
}
8566

86-
if (array_key_exists('items', $schema) && is_array($schema['items'])) {
87-
$schema['items'] = $this->adaptSchema($schema['items']);
67+
// Reduce the array type to a single not-nullable type
68+
if (isset($schema['type']) && is_array($schema['type'])) {
69+
foreach ($schema['type'] as $type) {
70+
if ($type !== 'null') {
71+
$schema['type'] = $type;
72+
break;
73+
}
74+
}
8875
}
8976

9077
return $schema;

0 commit comments

Comments
 (0)