Skip to content

Commit 0201a9e

Browse files
committed
use internal package
1 parent 2213024 commit 0201a9e

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.4",
20-
"adhocore/json-fixer": "^1.0",
20+
"cortexphp/json-repair": "^0.2",
2121
"cortexphp/json-schema": "^1.0",
2222
"cortexphp/model-info": "^0.3",
2323
"illuminate/collections": "^12.0",

src/OutputParsers/JsonOutputParser.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
use Override;
88
use JsonException;
9-
use Ahc\Json\Fixer;
109
use Cortex\LLM\Data\ChatGeneration;
1110
use Cortex\LLM\Data\ChatGenerationChunk;
1211
use Cortex\Exceptions\OutputParserException;
1312

13+
use function Cortex\JsonRepair\json_repair;
14+
1415
class JsonOutputParser extends AbstractOutputParser
1516
{
1617
protected const array PATTERNS = [
@@ -80,7 +81,10 @@ protected function repairJson(string $json): string
8081
// Note: This only works for top level at the moment.
8182
$json = preg_replace('/\{(\s*"\w+"\s*:\s*)\{[^}]*?\}\}/', '{$1{}}', $json);
8283

83-
return new Fixer()->silent()->fix((string) $json);
84+
return json_repair(
85+
json: $json,
86+
omitEmptyValues: true,
87+
);
8488
}
8589

8690
#[Override]

tests/Unit/OutputParsers/JsonOutputParserTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,7 @@
100100
'{"foo": "bar", "baz": {"hel',
101101
[
102102
'foo' => 'bar',
103-
'baz' => [
104-
'hel' => null,
105-
],
103+
'baz' => [],
106104
],
107105
],
108106
[

tests/Unit/OutputParsers/StructuredOutputParserTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Cortex\Tests\Unit\OutputParsers;
66

77
use Cortex\JsonSchema\Schema;
8-
use Cortex\Exceptions\OutputParserException;
98
use Cortex\OutputParsers\StructuredOutputParser;
109
use Cortex\JsonSchema\Exceptions\SchemaException;
1110

@@ -86,12 +85,13 @@
8685
expect($result['items'][1]['value'])->toBe('second');
8786
});
8887

89-
test('it throws exception for invalid JSON', function (): void {
88+
test('it handles invalid JSON', function (): void {
9089
$parser = new StructuredOutputParser(
9190
Schema::object()->properties(
9291
Schema::string('foo'),
9392
),
9493
);
94+
9595
$output = <<<'OUTPUT'
9696
```json
9797
{
@@ -101,7 +101,9 @@
101101
```
102102
OUTPUT;
103103

104-
expect(fn(): array => $parser->parse($output))->toThrow(OutputParserException::class);
104+
$result = $parser->parse($output);
105+
106+
expect($result['foo'])->toBe('bar');
105107
});
106108

107109
test('it handles different data types correctly', function (): void {

0 commit comments

Comments
 (0)