Skip to content

fix: use strict=False in json.loads for structured output parsing#152

Merged
Kamilbenkirane merged 1 commit into
mainfrom
fix/json-loads-strict-mode
Feb 20, 2026
Merged

fix: use strict=False in json.loads for structured output parsing#152
Kamilbenkirane merged 1 commit into
mainfrom
fix/json-loads-strict-mode

Conversation

@Kamilbenkirane

Copy link
Copy Markdown
Member

Summary

  • Fix json.loads strict mode crashing structured output parsing across all providers
  • LLM-generated structured output (e.g. thinking fields with literal \n) causes Invalid control character errors on the second JSON decode in parse_output()
  • Changed json.loads(content)json.loads(content, strict=False) in all 8 provider parse_output methods

Files changed

  • providers/anthropic/messages/parameters.py
  • providers/cohere/chat/parameters.py
  • providers/deepseek/chat/parameters.py
  • providers/google/generate_content/parameters.py
  • providers/google/interactions/parameters.py
  • providers/groq/chat/parameters.py
  • providers/mistral/chat/parameters.py
  • providers/moonshot/chat/parameters.py

Why not centralized

_transform_output in client.py is a generic mapper dispatcher — adding JSON-specific pre-processing there would couple the dispatcher to one mapper's implementation detail. The fix belongs where json.loads is called.

Test plan

  • All 472 unit tests pass
  • Coverage threshold met (80%+)
  • mypy passes
  • Ruff lint and format pass

LLM-generated structured output (e.g. thinking fields) can contain
literal control characters like \n. After the first JSON decode from
the HTTP response, these become raw control characters in Python
strings. The second json.loads() in parse_output() then rejects them
under default strict=True mode.

Fix by passing strict=False to json.loads in all 8 provider
parse_output methods.
@Kamilbenkirane Kamilbenkirane merged commit 1d4b330 into main Feb 20, 2026
11 checks passed
@Kamilbenkirane Kamilbenkirane mentioned this pull request Feb 24, 2026
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.

1 participant