fix: strip markdown code fences from JSON responses in OpenAIGenericClient#1291
Open
devmao wants to merge 1 commit intogetzep:mainfrom
Open
fix: strip markdown code fences from JSON responses in OpenAIGenericClient#1291devmao wants to merge 1 commit intogetzep:mainfrom
devmao wants to merge 1 commit intogetzep:mainfrom
Conversation
…lient
Some LLMs (especially via OpenRouter or other proxy backends) wrap their
JSON response in markdown code fences (e.g. ```json ... ```) even
when response_format={"type": "json_object"} is requested, causing
json.loads() to fail.
Strip a leading code fence and its closing backticks before parsing so
the client is robust to this behaviour.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Some LLMs (especially non-OpenAI models accessed via OpenRouter or other proxy backends) wrap their JSON output in markdown code fences — e.g.
json\n{...}\n— even whenresponse_format={"type": "json_object"}is requested. This causesjson.loads()to raise aJSONDecodeErrorand the call to fail.This fix strips a leading code fence and its closing backticks from the response content before parsing, making
OpenAIGenericClientrobust to this behaviour.Type of Change
Objective
N/A (bug fix)
Testing
Added
tests/llm_client/test_openai_generic_client.pywith four cases:json\n{...}\nfence\n{...}\nplain fence (no language tag)Breaking Changes
Checklist
make lintpasses)Related Issues
N/A