Learning resourcesReviewed · Pydantic

Pydantic AI: Raise Content Filter Error

Raise Content Filter Error is a Pydantic AI guide for Python agent developers who want filtered replies to raise exceptions, but it depends on responses being marked content_filter. It explains how RaiseContentFilterError also catches filtered responses containing partial text or refusal text and preserves the response in the exception body.

Researched 15 Nov 2026

In short.

Raise Content Filter Error is a Pydantic AI guide for Python developers who need explicit handling of filtered model replies, but it applies only to responses marked finish_reason='content_filter'. It documents an opt-in capability that raises ContentFilterError even when a filtered response contains text, which Pydantic AI would normally return as output. The full response remains available in serialized form through the exception body. The example was not executed; package-version requirements and real-provider compatibility are unknown.

Researched and written by an AI model and checked by a second AI model. How it works

Coverage includes the complete selected page at [https://pydantic.dev/docs/ai/capabilities/raise-content-filter-error/index.md], including its example, behavior explanation, default identifier and documentation-index/request-parameter notices. Linked API references, model documentation, capability documentation, merge rules and the documentation index are excluded. Nothing was executed. Installation, provider interoperability and runtime behavior remain UNVERIFIED; costs and licensing are unknown.

The overview

identity

This documentation page explains RaiseContentFilterError, a capability attached to a Pydantic AI Agent. It is a focused guide to one response-handling feature. The original page is [https://pydantic.dev/docs/ai/capabilities/raise-content-filter-error/index.md].

assessment

The guide is useful when an application needs every response marked content_filter to enter its exception-handling path. Its main practical distinction is how it handles partial replies and refusals: enabling the capability makes those filtered responses raise even though they contain text. Behavior with actual providers remains UNVERIFIED.

users

The likely audience is Python developers building Pydantic AI agents, especially those who need to distinguish filtered completions from ordinary output. Beginners need familiarity with imports, object construction and try/except. Experienced developers get precise details about the finish reason, exception body and capability identifier.

capabilities

RaiseContentFilterError turns every model response with finish_reason='content_filter' into ContentFilterError, including responses containing partial text or refusal text. When it raises, the complete ModelResponse is serialized into ContentFilterError.body, keeping the returned text available for inspection.

The capability has the default id 'raise_content_filter_error'. The page states that two instances merge without producing a duplicate-id error.

Using it

requirements

Following the example requires Python and a Pydantic AI release exposing Agent, RaiseContentFilterError and ContentFilterError. The demonstration also uses FunctionModel and model-message classes. The page provides no installation command, supported Python version or minimum Pydantic AI version. Compatibility with a particular installed release is unknown.

mechanism

By default, Pydantic AI raises ContentFilterError when a response marked content_filter is empty. If that response includes partial text or refusal text, the text normally becomes ordinary agent output. This capability extends the error condition to those nonempty responses.

The documented trigger is the finish_reason value. The page does not establish detection of refusals or filtered content when that marker is absent or different.

setup

Import RaiseContentFilterError from pydantic_ai.capabilities and ContentFilterError from pydantic_ai.exceptions. Pass capabilities=[RaiseContentFilterError()] when constructing Agent, then catch ContentFilterError around the agent call. The example uses agent.run_sync(...). Provider credentials and connection setup are not covered.

example

The example defines a Python callback that returns a ModelResponse containing refusal text, finish_reason='content_filter', and matching provider_details. It supplies that callback through FunctionModel and enables RaiseContentFilterError on the Agent.

A try/except block catches ContentFilterError and prints its message. The page shows the expected message as: Content filter triggered. Finish reason: 'content_filter'. This is an illustrative response constructed by the example, and the code was not executed.

Assessment & evidence

limitations

The demonstrated behavior depends on a response carrying the exact content_filter finish reason. The page does not establish provider coverage, streaming behavior, asynchronous usage or interactions with other capabilities. Detailed merge rules are linked elsewhere and are outside this page.

The capability preserves the response for inspection, but the page does not demonstrate reading or decoding ContentFilterError.body. No benchmarks or real-provider results are supplied.

reasoning

The documented behavior suggests a clear application-design benefit: filtered responses can consistently reach an exception handler regardless of whether they contain text. This can help an application decide how to present a filtered result. The tradeoff is that callers must handle exceptions for partial replies and refusals that would otherwise arrive as output.

alternatives

The documented alternative is Pydantic AI's default behavior: empty filtered responses raise, while filtered responses containing text become output. That may fit applications that want to present the returned partial reply or refusal directly. The page provides no comparison with other libraries or filtering approaches.

evidence

The evidence is the complete documentation page at [https://pydantic.dev/docs/ai/capabilities/raise-content-filter-error/index.md], retained as selected-page.txt. It contains the behavior explanation, configuration example, expected exception message and default capability identifier.

The example constructs its own filtered response. It illustrates the documented handling path but does not establish how any external model provider populates finish reasons or returns filtered content.

Access & freshness

costs

The page provides no pricing, provider charges or cost measurements. Its example supplies a response through a Python callback and shows no external provider request. Costs for using the capability in a deployed application are unknown, and its runtime overhead is not assessed.

rights

The selected page states no licence or redistribution terms for its documentation or example code. Reuse and redistribution permissions are unknown from this evidence.

freshness

The supplied page was captured at 2026-11-15T08:29:18.250Z with source revision sha256:4435aab47a9299cd4df8b32c8b0238dc172700f366e3f7e1323d5b02db150fbd. It states no publication date, update date or associated package version. The capture date does not establish when the capability became available.

actions

Before adoption, confirm that your installed Pydantic AI release exposes the capability and that your chosen provider produces the documented finish reason. Decide how your application should handle ContentFilterError and use its preserved response.

Useful evaluation cases include ordinary replies, empty filtered replies, filtered partial replies and filtered refusals. Evaluate the returned exception body as well as the visible output. These are suggested next steps; no runtime evaluation was performed.

Evidence for this report

selected-page.txtselected · checked 2026-11-15