Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/features/core/output_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ output_type = JsonSchema(schema_dict)
```

`JsonSchema` accepts two optional parameters:
- `whitespace_pattern` (defaults to `None`): specifies the pattern to use for JSON syntactic whitespace. If none is provided, the default permissive JSON whitespace rules are used.
- `whitespace_pattern` (defaults to `None`): specifies the pattern to use for JSON syntactic whitespace. If none is provided, the default permissive JSON whitespace rules are used. It is applied by the `outlines_core` backend; the `llguidance` and `xgrammar` backends do not support it and raise an error if one is set.
- `ensure_ascii` (defaults to `True`): defines the value to use for the argument `ensure_ascii` of the `json.dumps` method. If false, non-ASCII characters will be turned into unicodes.

### Regex Patterns
Expand Down
8 changes: 7 additions & 1 deletion src/outlines/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def get_json_schema_logits_processor(
backend_name: str | None,
model: SteerableModel,
json_schema: str,
whitespace_pattern: str | None = None,
) -> LogitsProcessorType:
"""Create a logits processor from a JSON schema.

Expand All @@ -70,6 +71,9 @@ def get_json_schema_logits_processor(
The Outlines model of the user.
json_schema: str
The JSON schema to create a logits processor from.
whitespace_pattern: str | None
The pattern to use to control the whitespace allowed between JSON
tokens. `None` uses the backend default.

Returns
-------
Expand All @@ -81,7 +85,9 @@ def get_json_schema_logits_processor(
backend_name or JSON_SCHEMA_DEFAULT_BACKEND,
model,
)
return backend.get_json_schema_logits_processor(json_schema)
return backend.get_json_schema_logits_processor(
json_schema, whitespace_pattern
)


def get_regex_logits_processor(
Expand Down
5 changes: 4 additions & 1 deletion src/outlines/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ class BaseBackend(ABC):

@abstractmethod
def get_json_schema_logits_processor(
self, json_schema: str
self, json_schema: str, whitespace_pattern: str | None = None
) -> LogitsProcessorType:
"""Create a logits processor from a JSON schema.

Parameters
----------
json_schema: str
The JSON schema to create a logits processor from.
whitespace_pattern: str | None
The pattern to use to control the whitespace allowed between JSON
tokens. `None` uses the backend default.

Returns
-------
Expand Down
10 changes: 9 additions & 1 deletion src/outlines/backends/llguidance.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,29 @@ def _create_llg_tokenizer(self, model: SteerableModel) -> "LLGTokenizer":
)

def get_json_schema_logits_processor(
self, json_schema: str
self, json_schema: str, whitespace_pattern: str | None = None
) -> LLGuidanceLogitsProcessor:
"""Create a logits processor from a JSON schema.

Parameters
----------
json_schema: str
The JSON schema to create a logits processor from.
whitespace_pattern: str | None
Not supported by the llguidance backend; must be `None`.

Returns
-------
LogitsProcessor
The logits processor to use to constrain the generation.

"""
if whitespace_pattern is not None:
raise NotImplementedError(
"The llguidance backend does not support the "
"`whitespace_pattern` argument. Use the `outlines_core` "
"backend to control JSON whitespace."
)
grammar_spec = self.llg.grammar_from("json_schema", json_schema)
return LLGuidanceLogitsProcessor(
grammar_spec, self.llg_tokenizer, self.tensor_library_name
Expand Down
9 changes: 7 additions & 2 deletions src/outlines/backends/outlines_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,26 @@ def __init__(self, model: SteerableModel):
)
self.tensor_library_name = model.tensor_library_name

def get_json_schema_logits_processor(self, json_schema: str):
def get_json_schema_logits_processor(
self, json_schema: str, whitespace_pattern: str | None = None
):
"""Create a logits processor from a JSON schema.

Parameters
----------
json_schema: str
The JSON schema to create a logits processor from.
whitespace_pattern: str | None
The pattern to use to control the whitespace allowed between JSON
tokens. `None` uses the outlines_core default.

Returns
-------
LogitsProcessor
The logits processor to use to constrain the generation.

"""
regex = build_regex_from_schema(json_schema)
regex = build_regex_from_schema(json_schema, whitespace_pattern)
return self.get_regex_logits_processor(regex)

def get_regex_logits_processor(self, regex: str):
Expand Down
10 changes: 9 additions & 1 deletion src/outlines/backends/xgrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,29 @@ def __init__(self, model: SteerableModel):
self.tensor_library_name = model.tensor_library_name

def get_json_schema_logits_processor(
self, json_schema: str
self, json_schema: str, whitespace_pattern: str | None = None
) -> XGrammarLogitsProcessor:
"""Create a logits processor from a JSON schema.

Parameters
----------
json_schema: str
The JSON schema to create a logits processor from.
whitespace_pattern: str | None
Not supported by the xgrammar backend; must be `None`.

Returns
-------
LogitsProcessor
The logits processor to use to constrain the generation.

"""
if whitespace_pattern is not None:
raise NotImplementedError(
"The xgrammar backend does not support the "
"`whitespace_pattern` argument. Use the `outlines_core` "
"backend to control JSON whitespace."
)
compiled_grammar = self.grammar_compiler.compile_json_schema(
json_schema
)
Expand Down
1 change: 1 addition & 0 deletions src/outlines/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __init__(
backend_name,
model,
term.schema,
term.whitespace_pattern,
)
else:
regex_string = to_regex(term)
Expand Down
8 changes: 8 additions & 0 deletions tests/backends/test_llguidance.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,11 @@ def test_llguidance_backend(model, tensor_library_name, json_schema, regex, cfg_
else:
response = generator("Create a character", max_tokens=20)
assert response[0] == "{"


def test_json_schema_logits_processor_rejects_whitespace_pattern():
"""The llguidance backend does not support whitespace_pattern and raises."""
backend = object.__new__(LLGuidanceBackend)
schema = '{"type": "object"}'
with pytest.raises(NotImplementedError, match="whitespace_pattern"):
backend.get_json_schema_logits_processor(schema, whitespace_pattern=" ")
42 changes: 42 additions & 0 deletions tests/backends/test_outlines_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,45 @@ def mock_vocabulary(eos_id, fmt_vocab):
assert captured["hi"] == [3]
assert len(captured) == 2
assert sum(len(ids) for ids in captured.values()) == 3


def test_json_schema_logits_processor_applies_whitespace_pattern():
"""A user-provided whitespace_pattern is forwarded to the regex builder.

Regression: the pattern was dropped before reaching the backend, so setting
it had no effect on the whitespace of the generated JSON.
"""
backend = object.__new__(OutlinesCoreBackend)
schema = '{"type": "object", "properties": {"a": {"type": "integer"}}}'

with (
patch(
"outlines.backends.outlines_core.build_regex_from_schema",
return_value=r"\{\}",
) as build_regex,
patch.object(
backend, "get_regex_logits_processor", return_value="processor"
),
):
backend.get_json_schema_logits_processor(schema, whitespace_pattern="")

build_regex.assert_called_once_with(schema, "")


def test_json_schema_logits_processor_defaults_whitespace_pattern_to_none():
"""When no whitespace_pattern is given, None is forwarded to the builder."""
backend = object.__new__(OutlinesCoreBackend)
schema = '{"type": "object"}'

with (
patch(
"outlines.backends.outlines_core.build_regex_from_schema",
return_value=r"\{\}",
) as build_regex,
patch.object(
backend, "get_regex_logits_processor", return_value="processor"
),
):
backend.get_json_schema_logits_processor(schema)

build_regex.assert_called_once_with(schema, None)
8 changes: 8 additions & 0 deletions tests/backends/test_xgrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,11 @@ def test_xgrammar_backend_invalid_model():
match="The xgrammar backend only supports Transformers and MLXLM models",
):
XGrammarBackend(model_llamacpp())


def test_json_schema_logits_processor_rejects_whitespace_pattern():
"""The xgrammar backend does not support whitespace_pattern and raises."""
backend = object.__new__(XGrammarBackend)
schema = '{"type": "object"}'
with pytest.raises(NotImplementedError, match="whitespace_pattern"):
backend.get_json_schema_logits_processor(schema, whitespace_pattern=" ")
Loading