Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/outlines/types/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,17 @@ def _handle_dict(args: tuple, recursion_depth: int) -> Sequence:
raise TypeError(f"Dict must have exactly two type arguments. Got {args}.")
# Add dict support with key:value pairs
key_type = _ensure_json_quoted(python_types_to_terms(args[0], recursion_depth + 1))
if isinstance(key_type, Regex) and key_type.pattern != types.string.pattern:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better solution would be to add a parameter to _ensure_json_quoted to indicate whether to add quotation marks to Regex terms on top of String terms (default would be False). That way we benefit from the recursive functioning of this function in case the term is an Alternative and we have a better containment of the issue (everything related to adding quotation marks in a single function).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, and it's more than a style improvement: the isinstance(key_type, Regex) check I had only handles a bare Regex, it misses Dict[Literal[1, 2, 3], str] entirely, since int Literal members go through _handle_literal into Alternatives([Regex("1"), Regex("2"), Regex("3")]), not a top-level Regex. Confirmed that produces unquoted numeric keys with the old code, same bug through a different path.

Added quote_regex to _ensure_json_quoted (d5c58af) and dropped the separate check, so it goes through the same Alternatives recursion String quoting already uses. Added a regression test for the Literal[int] case specifically. Full test_dsl.py passes except two pre-existing, unrelated failures on this Windows box (temp-file PermissionError in the from_file tests, reproduces identically on main without my changes).

# JSON object keys must always be double-quoted strings, even when
# the Python key type is not `str` (e.g. `Dict[int, str]`,
# `Dict[date, str]`). `_ensure_json_quoted` only quotes `String`/
# `Alternatives` terms (from `Literal`/`Enum`); bare `Regex` terms
# like `types.integer`, `types.number`, `types.boolean`, `types.date`
# etc. are otherwise left unquoted, which produces a regex that
# matches invalid JSON (e.g. `{1:"a"}` instead of `{"1":"a"}`).
# `types.string` is excluded since its pattern is already
# self-quoted (`"[^"]*"`).
key_type = Sequence([String('"'), key_type, String('"')])
value_type = _ensure_json_quoted(python_types_to_terms(args[1], recursion_depth + 1))
return Sequence(
[
Expand Down
39 changes: 34 additions & 5 deletions tests/types/test_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,21 +839,50 @@ def test_dsl_handle_dict():
incorrect_dict_type = dict[int, str, int]
_handle_dict(get_args(incorrect_dict_type), recursion_depth=0)

# correct type
dict_type = dict[int, str]
# correct type with a str key: no extra quoting needed, types.string
# is already self-quoted
dict_type = dict[str, int]
result = _handle_dict(get_args(dict_type), recursion_depth=0)
assert isinstance(result, Sequence)
assert len(result.terms) == 3
assert result.terms[0] == String("{")
assert isinstance(result.terms[1], Optional)
assert isinstance(result.terms[1].term, Sequence)
assert len(result.terms[1].term.terms) == 4
assert result.terms[1].term.terms[0] == types.integer
assert result.terms[1].term.terms[0] == types.string
assert result.terms[1].term.terms[1] == String(":")
assert result.terms[1].term.terms[2] == types.string
assert result.terms[1].term.terms[3] == KleeneStar(Sequence([String(", "), types.integer, String(":"), types.string]))
assert result.terms[1].term.terms[2] == types.integer
assert result.terms[1].term.terms[3] == KleeneStar(Sequence([String(", "), types.string, String(":"), types.integer]))
assert result.terms[2] == String("}")

# non-str key (e.g. int): JSON object keys must always be double-quoted
# strings, so the bare `types.integer` regex must be wrapped in quotes
# even though the Python key type is `int`.
dict_type = dict[int, str]
result = _handle_dict(get_args(dict_type), recursion_depth=0)
quoted_int_key = Sequence([String('"'), types.integer, String('"')])
assert result.terms[1].term.terms[0] == quoted_int_key
assert result.terms[1].term.terms[2] == types.string


def test_dsl_handle_dict_non_string_key_produces_valid_json():
"""Regression test: Dict[int, str] (and other non-str key types) must
only match strings that are valid JSON, i.e. the key must be quoted."""
import json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep all the imports at the top of the file when possible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to the module-level imports (json and Literal were both already imported at the top).


dict_type = dict[int, str]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's duplicating what's above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, dropped it. The dict[int, str] key-quoting is already covered by the structural assertion in test_dsl_handle_dict, so this one was redundant.

result = _handle_dict(get_args(dict_type), recursion_depth=0)
pattern = to_regex(result)

# unquoted key: not valid JSON, must not match
assert _re.fullmatch(pattern, '{1:"a"}') is None
with pytest.raises(json.JSONDecodeError):
json.loads('{1:"a"}')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does not make sense, we don't want to test that the json library works well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, removed the json.loads / JSONDecodeError lines. Kept just the assertion on our generated term structure.


# quoted key: valid JSON, must match
assert _re.fullmatch(pattern, '{"1":"a"}') is not None
json.loads('{"1":"a"}') # does not raise


def test_ensure_json_quoted_string():
"""String terms are wrapped in double-quote delimiters."""
Expand Down
Loading