Add `RaiseContentFilterError` capability to raise content filter error for non-empty content filter responses

Merged
DS
4mo ago
+274-3

Closes #5221

Summary:

  • Add RaiseContentFilterError, a zero-config capability that raises ContentFilterError for normalized finish_reason='content_filter' responses.
  • Preserve the serialized ModelResponse in ContentFilterError.body, including partial/refusal text.
  • Document the opt-in behavior and update the in-repo agent skill reference.

Test plan:

  • uv run pytest tests/test_agent.py::test_central_content_filter_handling tests/test_agent.py::test_central_content_filter_with_partial_content tests/test_agent.py::test_raise_content_filter_error_capability_with_partial_content tests/test_agent.py::test_raise_content_filter_error_capability_noop_for_other_finish_reason tests/test_capabilities.py::test_capability_types tests/test_capabilities.py::test_model_json_schema_with_capabilities
  • uv run pytest tests/test_examples.py -k capabilities
  • env PYRIGHT_PYTHON_IGNORE_WARNINGS=1 uv run pyright pydantic_ai_slim/pydantic_ai/capabilities/content_filter.py pydantic_ai_slim/pydantic_ai/capabilities/__init__.py pydantic_ai_slim/pydantic_ai/exceptions.py tests/test_agent.py tests/test_capabilities.py
  • make format
  • make lint

Default agent behavior is unchanged in this PR. This gives users an opt-in bridge before the future central default semantic change.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.