Skip to content

fix(core): support CRLF line endings in extract_json_from_str codeblock regex - #8081

Open
Xayar145 wants to merge 2 commits into
microsoft:mainfrom
Xayar145:fix/extract-json-crlf-line-endings
Open

fix(core): support CRLF line endings in extract_json_from_str codeblock regex#8081
Xayar145 wants to merge 2 commits into
microsoft:mainfrom
Xayar145:fix/extract-json-crlf-line-endings

Conversation

@Xayar145

Copy link
Copy Markdown

Summary

Supports CRLF (\r\n) line endings in autogen_core.utils.extract_json_from_str:

  • Updated regex pattern to r"```(?:\s*([\w\+\-]+))?\r?\n([\s\S]*?)```" to match code blocks formatted with Windows/network CRLF line endings.
  • Strips whitespace around content before JSON decoding.
  • Added unit test test_extract_json_from_str_crlf in test_json_extraction.py.

Motivation

When JSON code blocks are received over network streams or generated on Windows environments with CRLF line endings, the previous regex pattern required strict LF (\n), causing extraction to fail and falling back to unparsed strings.

Changes

  • python/packages/autogen-core/src/autogen_core/utils/_load_json.py: Match optional \r in codeblock regex.
  • python/packages/autogen-core/tests/test_json_extraction.py: Added test case with CRLF line endings.

Tests

  • Added test_extract_json_from_str_crlf.

@Xayar145

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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