Skip to content

Fix the JsonSchema whitespace_pattern being ignored by the backend#1924

Merged
RobinPicard merged 2 commits into
dottxt-ai:mainfrom
dhanavanthesh:fix/json-schema-whitespace-pattern
Jul 20, 2026
Merged

Fix the JsonSchema whitespace_pattern being ignored by the backend#1924
RobinPicard merged 2 commits into
dottxt-ai:mainfrom
dhanavanthesh:fix/json-schema-whitespace-pattern

Conversation

@dhanavanthesh

Copy link
Copy Markdown
Contributor

Problem

When you set whitespace_pattern on a JsonSchema output type, it has no effect during generation on the default outlines_core backend. The JSON is always produced with the default whitespace, whatever pattern you pass.

The cause is a dropped argument. When the generator builds the logits processor for a JsonSchema, it passes only term.schema to the backend and never passes term.whitespace_pattern (src/outlines/generator.py). The backend then calls build_regex_from_schema(json_schema) without the pattern (src/outlines/backends/outlines_core.py). Calling to_regex(JsonSchema(...)) directly does apply the pattern, so the two paths disagree.

whitespace_pattern is a documented parameter (docs/features/core/output_types.md) and worked in v0, where it was added to control JSON whitespace (#916). It stopped being applied when JSON-schema handling moved to the backend layer in the v1 refactor.

Fix

Pass whitespace_pattern through to the backend so it is actually used:

  • The generator now passes term.whitespace_pattern when it builds the JSON-schema logits processor.
  • get_json_schema_logits_processor (the backend dispatcher, BaseBackend, and OutlinesCoreBackend) takes an optional whitespace_pattern and forwards it to build_regex_from_schema.
  • The llguidance and xgrammar backends build the grammar their own way and cannot use this pattern, so they raise NotImplementedError if one is set instead of ignoring it.

The default (whitespace_pattern=None) behaves exactly as before.

Tests

Two new tests in tests/backends/test_outlines_core.py:

  • one checks that a pattern you pass reaches build_regex_from_schema,
  • one checks that the default passes None.

Both fail on main and pass with this change (verified by stashing the fix). The tests/backends/test_outlines_core.py suite passes (one MLX test is skipped). ruff and mypy are clean.

Docs

docs/features/core/output_types.md now says whitespace_pattern is applied by the outlines_core backend, and that llguidance / xgrammar do not support it.

The whitespace_pattern set on a JsonSchema output type was not passed to
the backend during generation, so it had no effect on the default
outlines_core backend even though to_regex already honors it. Thread the
whitespace_pattern through the backend interface and forward it to
build_regex_from_schema.

The llguidance and xgrammar backends raise NotImplementedError when a
pattern is set, since they compile the schema with their own grammar
engines and cannot apply it.
@github-actions

Copy link
Copy Markdown

📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1924/

Preview updates automatically with each commit.

…ends

The backends raise NotImplementedError when whitespace_pattern is set, but
only the outlines_core backend had a test. Add a regression test for each so
the unsupported whitespace path is exercised.

@RobinPicard RobinPicard left a comment

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.

Great PR, thanks! And good idea to raise an error when the parameter is used with xgrammar/llguidance instead of silently ignoring it.

@RobinPicard
RobinPicard merged commit dd180da into dottxt-ai:main Jul 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants