Skip to content

Commit

Permalink
lf.structured.ValueJsonRepr.parse to pass through keyword args to `…
Browse files Browse the repository at this point in the history
…pg.from_json_str`.

This allows passing `force_dict=True` when dealing with hallucinated `_type` field.

PiperOrigin-RevId: 572954123
  • Loading branch information
daiyip authored and langfun authors committed Oct 12, 2023
1 parent 199fd49 commit 639b51e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion langfun/core/structured/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def parse(self, text: str, schema: Schema | None = None, **kwargs) -> Any:
del schema
try:
text = self.cleanup_json(text)
v = pg.from_json_str(text)
v = pg.from_json_str(text, **kwargs)
except Exception as e:
raise JsonError(text, e) # pylint: disable=raise-missing-from

Expand Down
5 changes: 5 additions & 0 deletions langfun/core/structured/schema_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ def test_value_repr(self):
def test_parse(self):
schema = schema_lib.Schema(int)
self.assertEqual(schema.parse('{"result": 1}'), 1)
schema = schema_lib.Schema(dict[str, int])
self.assertEqual(
schema.parse(
'{"result": {"_type": "Unknown", "x": 1}}}', force_dict=True),
dict(x=1))
with self.assertRaisesRegex(
schema_lib.SchemaError, 'Expect .* but encountered .*'):
schema.parse('{"result": "def"}')
Expand Down

0 comments on commit 639b51e

Please sign in to comment.