Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pydantic_ai_slim/pydantic_ai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,10 @@ def validate_empty_kwargs(_kwargs: dict[str, Any]) -> None:
def strip_markdown_fences(text: str) -> str:
if text.startswith('{'):
return text

regex = r'```(?:\w+)?\n(\{.*\})\n```'
regex = r'```(?:\w+)?\s*(\{.*\})\s*```'
match = re.search(regex, text, re.DOTALL)
if match:
return match.group(1)

return match.group(1).strip()
return text


Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,15 @@ def test_strip_markdown_fences():
)
assert strip_markdown_fences('No JSON to be found') == 'No JSON to be found'

assert strip_markdown_fences('```json {"foo": "bar"}```') == '{"foo": "bar"}'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do models ever do this, without a newline following the fence?

assert strip_markdown_fences('```json\n\n{"foo": "bar"}\n\n```') == '{"foo": "bar"}'
assert strip_markdown_fences('```\n{"foo": "bar"}```') == '{"foo": "bar"}'
assert strip_markdown_fences('```json \n{"foo": "bar"}\n ```') == '{"foo": "bar"}'
assert (
strip_markdown_fences('```json\n{"foo": "bar"}\n```\nSome text\n```json\n{"baz": "qux"}\n```')
== '{"foo": "bar"}\n```\nSome text\n```json\n{"baz": "qux"}'
)


def test_validate_empty_kwargs_empty():
"""Test that empty dict passes validation."""
Expand Down