[GH-ISSUE #2893] [Engine] Opt-in LaTeX-safe JSON recovery for json_object responses (LightRAG VLM) #1168

Closed
opened 2026-11-04 01:49:03 +07:00 by bsakaguchi · 9 comments
Owner

Originally created by @BoysGameStudio on GitHub (Oct 17, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/2893

Summary

When a vision-language model generates JSON containing an unescaped LaTeX
backslash, Strata's response_format: {"type":"json_object"} validation
rejects the completion with HTTP 502 (structured_output_failed).

This breaks LightRAG's multimodal PDF ingestion because LightRAG already
implements tolerant JSON parsing and LaTeX escape recovery, but Strata
rejects the response before LightRAG can access the generated text.

The issue has been reproduced using the same image and prompt.

Environment

  • Strata v0.1.44.1 (Strata-NVFP4 fork)
  • Qwen3.8-Flash-Next NVFP4
  • GPU vision enabled
  • LightRAG v1.5.7
  • MinerU PDF pipeline
  • OpenAI-compatible Chat Completions API

Minimal Reproduction

Model-generated content:

{"name":"alpha coefficient","type":"diagram","description":"Coefficient \alpha_0"}

The sequence \a is not a valid JSON escape.

This can also be reproduced directly against Strata's validator:

from serve.structured import _ObjectOnly, validated_json

raw = r'{"name":"alpha coefficient","type":"diagram","description":"Coefficient \alpha_0"}'

print(validated_json(raw, _ObjectOnly(), "stop"))

Actual Behavior

  • Strict JSON parsing fails.
  • Strata returns HTTP 502 (structured_output_failed).
  • The original generated response is unavailable to LightRAG.
  • LightRAG retries the failed provider request.
  • The document eventually enters the FAILED state.

Expected Behavior

An optional recovery mode could repair unambiguous LaTeX escaping errors
before strict validation.

For example:

{"name":"alpha coefficient","type":"diagram","description":"Coefficient \\alpha_0"}

The repaired output should still pass strict JSON validation before
being returned to the client.

Proposed Solution

Introduce an opt-in, conservative JSON recovery mechanism for
response_format=json_object.

Suggested behavior:

  1. Preserve existing strict validation by default.
  2. Attempt limited LaTeX-aware repair when explicitly enabled.
  3. Validate the repaired JSON using the existing validator.
  4. Return canonical JSON only after successful validation.
  5. Continue returning HTTP 502 for unrecoverable output.
  6. Preserve existing json_schema behavior.

The implementation must also account for LaTeX commands such as:

  • \alpha
  • \beta
  • \frac
  • \tau
  • \nabla

Some commands conflict with valid JSON escape sequences, so simply
doubling every backslash is unsafe.

Regression tests should cover legitimate JSON escapes, already-correct
LaTeX escaping, duplicate keys, invalid constants, and truncated output.

Constrained decoding may prevent syntactically invalid JSON, but it does
not necessarily preserve LaTeX semantics when a model emits JSON-valid
escape sequences.

This feature would improve compatibility with LightRAG and other
applications processing mathematical or technical documents.

Retest (2026-10-21)

Retested on Strata 0.2.0-beta (source, tag v0.2.0-beta) and on the current release (Strata 0.2.1.4
source build, engine strata 0.1.46.3). Still reproduces on both; serve/structured.py is unchanged
since v0.1.44.1.

  • GPU: RTX 5090 32 GB, driver 615.71.10. Memory: 121 GiB. System: Ubuntu 26.04.1, kernel 7.0.0-38.
  • What I see: with response_format: {"type":"json_object"}, a completion containing
    {"description":"$\alpha_0 > 1$"} gives HTTP 502 structured_output_failed (validator:
    Invalid \escape), and the text never reaches the client. Reproduced on both with the built-in mock
    engine (--engine mock --script), which is the same validation path. Not reproduced with a real
    model: my runs escaped the backslashes correctly, so the rejection comes from the validator, not the
    model.
  • The 502 says nothing about what the model wrote, so the caller cannot recover the text itself.
Originally created by @BoysGameStudio on GitHub (Oct 17, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/2893 ## Summary When a vision-language model generates JSON containing an unescaped LaTeX backslash, Strata's `response_format: {"type":"json_object"}` validation rejects the completion with HTTP 502 (`structured_output_failed`). This breaks LightRAG's multimodal PDF ingestion because LightRAG already implements tolerant JSON parsing and LaTeX escape recovery, but Strata rejects the response before LightRAG can access the generated text. The issue has been reproduced using the same image and prompt. ## Environment - Strata v0.1.44.1 (Strata-NVFP4 fork) - Qwen3.8-Flash-Next NVFP4 - GPU vision enabled - LightRAG v1.5.7 - MinerU PDF pipeline - OpenAI-compatible Chat Completions API ## Minimal Reproduction Model-generated content: ```text {"name":"alpha coefficient","type":"diagram","description":"Coefficient \alpha_0"} ``` The sequence `\a` is not a valid JSON escape. This can also be reproduced directly against Strata's validator: ```python from serve.structured import _ObjectOnly, validated_json raw = r'{"name":"alpha coefficient","type":"diagram","description":"Coefficient \alpha_0"}' print(validated_json(raw, _ObjectOnly(), "stop")) ``` ### Actual Behavior - Strict JSON parsing fails. - Strata returns HTTP 502 (`structured_output_failed`). - The original generated response is unavailable to LightRAG. - LightRAG retries the failed provider request. - The document eventually enters the FAILED state. ### Expected Behavior An optional recovery mode could repair unambiguous LaTeX escaping errors before strict validation. For example: ```json {"name":"alpha coefficient","type":"diagram","description":"Coefficient \\alpha_0"} ``` The repaired output should still pass strict JSON validation before being returned to the client. ## Proposed Solution Introduce an opt-in, conservative JSON recovery mechanism for `response_format=json_object`. Suggested behavior: 1. Preserve existing strict validation by default. 2. Attempt limited LaTeX-aware repair when explicitly enabled. 3. Validate the repaired JSON using the existing validator. 4. Return canonical JSON only after successful validation. 5. Continue returning HTTP 502 for unrecoverable output. 6. Preserve existing `json_schema` behavior. The implementation must also account for LaTeX commands such as: - `\alpha` - `\beta` - `\frac` - `\tau` - `\nabla` Some commands conflict with valid JSON escape sequences, so simply doubling every backslash is unsafe. Regression tests should cover legitimate JSON escapes, already-correct LaTeX escaping, duplicate keys, invalid constants, and truncated output. ## Related Issues and PRs - Strata #2368 — Grammar-Constrained Sampling - Strata #1328 — Token Masks for JSON Formats with MTP - LightRAG PR #3238 — Repair LaTeX Escape Damage in LLM JSON Responses https://github.com/HKUDS/LightRAG/pull/3238 - LightRAG PR #3242 — Retry Malformed Multimodal Analysis JSON https://github.com/HKUDS/LightRAG/pull/3242 Constrained decoding may prevent syntactically invalid JSON, but it does not necessarily preserve LaTeX semantics when a model emits JSON-valid escape sequences. This feature would improve compatibility with LightRAG and other applications processing mathematical or technical documents. ## Retest (2026-10-21) Retested on Strata 0.2.0-beta (source, tag `v0.2.0-beta`) and on the current release (Strata 0.2.1.4 source build, engine `strata 0.1.46.3`). Still reproduces on both; `serve/structured.py` is unchanged since v0.1.44.1. - GPU: RTX 5090 32 GB, driver 615.71.10. Memory: 121 GiB. System: Ubuntu 26.04.1, kernel 7.0.0-38. - What I see: with `response_format: {"type":"json_object"}`, a completion containing `{"description":"$\alpha_0 > 1$"}` gives HTTP 502 `structured_output_failed` (validator: `Invalid \escape`), and the text never reaches the client. Reproduced on both with the built-in mock engine (`--engine mock --script`), which is the same validation path. Not reproduced with a real model: my runs escaped the backslashes correctly, so the rejection comes from the validator, not the model. - The 502 says nothing about what the model wrote, so the caller cannot recover the text itself.
Author
Owner

@Niko1221 commented on GitHub (Oct 19, 2026):

Thanks for taking the time to report this.

We're resetting the tracker for 0.2.0-beta. There are more than 1,300 open issues and pull requests, too many for us to read properly, so we're closing everything that isn't being worked on right now. Closed here does not mean rejected.

To reopen it takes about two minutes:

  1. Re-test on 0.2.0-beta. It may already be fixed. Write down what you see.
  2. Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other].
  3. Fill in the parts of the issue form: what happened, the Strata version, your graphics card, memory and system.
  4. Comment /reopen. A check runs and either reopens the issue or lists what is still missing.

The rules are in CONTRIBUTING.md.

<!-- gh-comment-id:6271347915 --> @Niko1221 commented on GitHub (Oct 19, 2026): Thanks for taking the time to report this. We're resetting the tracker for 0.2.0-beta. There are more than 1,300 open issues and pull requests, too many for us to read properly, so we're closing everything that isn't being worked on right now. Closed here does not mean rejected. To reopen it takes about two minutes: 1. Re-test on 0.2.0-beta. It may already be fixed. Write down what you see. 2. Start the title with its area: [Engine], [GUI], [TUI], [Benchmark], [Docs] or [Other]. 3. Fill in the parts of the issue form: what happened, the Strata version, your graphics card, memory and system. 4. Comment `/reopen`. A check runs and either reopens the issue or lists what is still missing. The rules are in [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md).
Author
Owner

@BoysGameStudio commented on GitHub (Oct 21, 2026):

Retested on a source build of the current release — still reproduces.

  • Strata: v0.2.1.4 source build, engine strata 0.1.46.3 (CUDA 13.4, sm_120). serve/structured.py
    is unchanged since v0.1.44.1.
  • GPU: RTX 5090 32 GB, driver 615.71.10. Memory: 121 GiB. System: Ubuntu 26.04.1, kernel 7.0.0-38.
  • What I see: with response_format: {"type":"json_object"}, a completion containing
    {"description":"$\alpha_0 > 1$"} gives HTTP 502 structured_output_failed and the text never
    reaches the client. Reproduced with the built-in mock engine (--engine mock --script), which is the
    same validation path. Not reproduced with a real model: my runs escaped the backslashes correctly, so
    the rejection comes from the validator, not the model.
  • Not tested against the beta binaries; this is a build of the release tag's source.
<!-- gh-comment-id:6314460599 --> @BoysGameStudio commented on GitHub (Oct 21, 2026): Retested on a source build of the current release — still reproduces. - Strata: v0.2.1.4 source build, engine `strata 0.1.46.3` (CUDA 13.4, sm_120). `serve/structured.py` is unchanged since v0.1.44.1. - GPU: RTX 5090 32 GB, driver 615.71.10. Memory: 121 GiB. System: Ubuntu 26.04.1, kernel 7.0.0-38. - What I see: with `response_format: {"type":"json_object"}`, a completion containing `{"description":"$\alpha_0 > 1$"}` gives HTTP 502 `structured_output_failed` and the text never reaches the client. Reproduced with the built-in mock engine (`--engine mock --script`), which is the same validation path. Not reproduced with a real model: my runs escaped the backslashes correctly, so the rejection comes from the validator, not the model. - Not tested against the beta binaries; this is a build of the release tag's source.
Author
Owner

@BoysGameStudio commented on GitHub (Oct 21, 2026):

/reopen

<!-- gh-comment-id:6314462850 --> @BoysGameStudio commented on GitHub (Oct 21, 2026): /reopen
Author
Owner

@github-actions[bot] commented on GitHub (Oct 21, 2026):

Not reopened yet. Still missing:

  • Re-test on Strata 0.2.0-beta and name that version in the issue. If it no longer happens, say so in a comment instead.

Edit the issue and comment /reopen again. Details: CONTRIBUTING.md

<!-- gh-comment-id:6314465900 --> @github-actions[bot] commented on GitHub (Oct 21, 2026): Not reopened yet. Still missing: - [ ] Re-test on Strata 0.2.0-beta and name that version in the issue. If it no longer happens, say so in a comment instead. Edit the issue and comment `/reopen` again. Details: [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md)
Author
Owner

@BoysGameStudio commented on GitHub (Oct 21, 2026):

/reopen

<!-- gh-comment-id:6314484654 --> @BoysGameStudio commented on GitHub (Oct 21, 2026): /reopen
Author
Owner

@github-actions[bot] commented on GitHub (Oct 21, 2026):

Not reopened yet. Still missing:

  • Re-test on Strata 0.2.0-beta and name that version in the issue. If it no longer happens, say so in a comment instead.

Edit the issue and comment /reopen again. Details: CONTRIBUTING.md

<!-- gh-comment-id:6314490493 --> @github-actions[bot] commented on GitHub (Oct 21, 2026): Not reopened yet. Still missing: - [ ] Re-test on Strata 0.2.0-beta and name that version in the issue. If it no longer happens, say so in a comment instead. Edit the issue and comment `/reopen` again. Details: [CONTRIBUTING.md](https://github.com/Niko1221/Strata/blob/main/CONTRIBUTING.md)
Author
Owner

@BoysGameStudio commented on GitHub (Oct 21, 2026):

/reopen

<!-- gh-comment-id:6314496253 --> @BoysGameStudio commented on GitHub (Oct 21, 2026): /reopen
Author
Owner

@github-actions[bot] commented on GitHub (Oct 21, 2026):

Reopened, thank you. A maintainer will pick it up from here.

<!-- gh-comment-id:6314498852 --> @github-actions[bot] commented on GitHub (Oct 21, 2026): Reopened, thank you. A maintainer will pick it up from here.
Author
Owner

@Niko1221 commented on GitHub (Oct 27, 2026):

Thank you. Fixed in 0.2.7: Opt-in LaTeX-safe JSON recovery for structured output.

Release: https://github.com/Niko1221/Strata/releases/tag/v0.2.7

<!-- gh-comment-id:6446021079 --> @Niko1221 commented on GitHub (Oct 27, 2026): Thank you. Fixed in 0.2.7: Opt-in LaTeX-safe JSON recovery for structured output. Release: https://github.com/Niko1221/Strata/releases/tag/v0.2.7
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#1168
No description provided.