Skip to content
Merged
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
34 changes: 4 additions & 30 deletions pydantic_ai_slim/pydantic_ai/_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ def __init__(
allows_image: bool,
):
super().__init__(
processor=PromptedOutputProcessor(processor),
processor=processor,
allows_deferred_tools=allows_deferred_tools,
allows_image=allows_image,
)
Expand All @@ -494,13 +494,6 @@ def build_instructions(cls, template: str, object_def: OutputObjectDefinition) -

return template.format(schema=json.dumps(schema))

def instructions(self, default_template: str) -> str: # pragma: no cover
"""Get instructions to tell model to output JSON matching the schema."""
template = self.template or default_template
object_def = self.object_def
assert object_def is not None
return self.build_instructions(template, object_def)


@dataclass(init=False)
class ToolOutputSchema(OutputSchema[OutputDataT]):
Expand Down Expand Up @@ -542,28 +535,6 @@ class BaseObjectOutputProcessor(BaseOutputProcessor[OutputDataT]):
object_def: OutputObjectDefinition


@dataclass(init=False)
class PromptedOutputProcessor(BaseObjectOutputProcessor[OutputDataT]):
wrapped: BaseObjectOutputProcessor[OutputDataT]

def __init__(self, wrapped: BaseObjectOutputProcessor[OutputDataT]):
self.wrapped = wrapped
super().__init__(object_def=wrapped.object_def)

async def process(
self,
data: str,
run_context: RunContext[AgentDepsT],
allow_partial: bool = False,
wrap_validation_errors: bool = True,
) -> OutputDataT:
text = _utils.strip_markdown_fences(data)

return await self.wrapped.process(
text, run_context, allow_partial=allow_partial, wrap_validation_errors=wrap_validation_errors
)


@dataclass(init=False)
class ObjectOutputProcessor(BaseObjectOutputProcessor[OutputDataT]):
outer_typed_dict_key: str | None = None
Expand Down Expand Up @@ -653,6 +624,9 @@ async def process(
Returns:
Either the validated output data (left) or a retry message (right).
"""
if isinstance(data, str):
data = _utils.strip_markdown_fences(data)

try:
output = self.validate(data, allow_partial)
except ValidationError as e:
Expand Down
6 changes: 4 additions & 2 deletions pydantic_ai_slim/pydantic_ai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,14 @@ def validate_empty_kwargs(_kwargs: dict[str, Any]) -> None:
raise exceptions.UserError(f'Unknown keyword arguments: {unknown_kwargs}')


_MARKDOWN_FENCES_PATTERN = re.compile(r'```(?:\w+)?\n(\{.*\})', flags=re.DOTALL)


def strip_markdown_fences(text: str) -> str:
if text.startswith('{'):
return text

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

Expand Down
1 change: 1 addition & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ def test_merge_json_schema_defs():
def test_strip_markdown_fences():
assert strip_markdown_fences('{"foo": "bar"}') == '{"foo": "bar"}'
assert strip_markdown_fences('```json\n{"foo": "bar"}\n```') == '{"foo": "bar"}'
assert strip_markdown_fences('```json\n{\n "foo": "bar"\n}') == '{\n "foo": "bar"\n}'
assert (
strip_markdown_fences('{"foo": "```json\\n{"foo": "bar"}\\n```"}')
== '{"foo": "```json\\n{"foo": "bar"}\\n```"}'
Expand Down