Skip to content

API Reference

This page documents the public API exported from azure_functions_validation.

from azure_functions_validation import (
    AdapterConformanceTests,
    AdapterValidationError,
    AggregatedValidationError,
    ErrorFormatter,
    HttpError,
    MalformedRequestError,
    PayloadTooLargeError,
    PydanticAdapter,
    ResponseValidationError,
    SerializationError,
    UnsupportedMediaTypeError,
    ValidationAdapter,
    validate_http,
)

Public surface

The package exports (__all__): validate_http, ResponseValidationError, SerializationError, MalformedRequestError, PayloadTooLargeError, UnsupportedMediaTypeError, ErrorFormatter, AdapterValidationError, AggregatedValidationError, ValidationAdapter, PydanticAdapter, AdapterConformanceTests, and HttpError. Pipeline internals (PipelineConfig, run_pipeline) are not public contracts.

validate_http

Decorator for validating HTTP request inputs and response outputs.

Parameters:

Name Type Description Default
body Any

Pydantic model for request body validation.

None
query Any

Pydantic model for query parameter validation.

None
path Any

Pydantic model for path parameter validation.

None
headers Any

Pydantic model for header validation.

None
request_model Any

Deprecated shorthand alias for body. Use body instead; passing request_model emits a DeprecationWarning.

None
response_model Any

Pydantic model for response validation.

None
adapter ValidationAdapter | None

Custom validation adapter (defaults to PydanticAdapter).

None
error_formatter ErrorFormatter | None

Per-handler custom error formatter.

None
expose_internal_errors bool

Unsafe opt-in that passes original server-side exceptions to error_formatter. Defaults to False so custom formatters receive a sanitized InternalServerError for 5xx errors.

False
status_code int

HTTP status code for successful responses (default 200). Use e.g. status_code=201 for creation endpoints.

200
legacy_loc bool

When True, error loc values omit the leading input-source segment (["email"] instead of ["body", "email"]). A one-cycle migration escape hatch; ignored when a custom adapter is supplied (configure that adapter directly).

False
response_by_alias bool

Serialize validated responses using field aliases.

True
response_exclude_none bool

Exclude fields whose value is None from validated responses.

False
response_exclude_unset bool

Exclude fields that were not explicitly set from validated responses.

False
max_body_bytes int | None

Maximum accepted request-body size in bytes. Disabled by default.

None
require_json_content_type bool

Require a JSON media type for non-empty configured bodies.

False
aggregate_validation_errors bool

Collect validation errors from every configured request source into one 422 response. Defaults to fail-fast behavior.

False

Returns:

Type Description
Callable[..., Any]

A decorator that wraps the handler with validation logic.

Source code in src/azure_functions_validation/decorator.py
def validate_http(
    *,
    body: Any = None,
    query: Any = None,
    path: Any = None,
    headers: Any = None,
    request_model: Any = None,
    response_model: Any = None,
    adapter: ValidationAdapter | None = None,
    error_formatter: ErrorFormatter | None = None,
    expose_internal_errors: bool = False,
    status_code: int = 200,
    legacy_loc: bool = False,
    response_by_alias: bool = True,
    response_exclude_none: bool = False,
    response_exclude_unset: bool = False,
    max_body_bytes: int | None = None,
    require_json_content_type: bool = False,
    aggregate_validation_errors: bool = False,
) -> Callable[..., Any]:
    """Decorator for validating HTTP request inputs and response outputs.

    Args:
        body: Pydantic model for request body validation.
        query: Pydantic model for query parameter validation.
        path: Pydantic model for path parameter validation.
        headers: Pydantic model for header validation.
        request_model: Deprecated shorthand alias for *body*. Use ``body`` instead;
            passing ``request_model`` emits a ``DeprecationWarning``.
        response_model: Pydantic model for response validation.
        adapter: Custom validation adapter (defaults to ``PydanticAdapter``).
        error_formatter: Per-handler custom error formatter.
        expose_internal_errors: Unsafe opt-in that passes original server-side
            exceptions to *error_formatter*. Defaults to ``False`` so custom
            formatters receive a sanitized ``InternalServerError`` for 5xx errors.
        status_code: HTTP status code for successful responses (default 200).
            Use e.g. ``status_code=201`` for creation endpoints.
        legacy_loc: When ``True``, error ``loc`` values omit the leading
            input-source segment (``["email"]`` instead of ``["body", "email"]``).
            A one-cycle migration escape hatch; ignored when a custom *adapter*
            is supplied (configure that adapter directly).
        response_by_alias: Serialize validated responses using field aliases.
        response_exclude_none: Exclude fields whose value is ``None`` from
            validated responses.
        response_exclude_unset: Exclude fields that were not explicitly set from
            validated responses.
        max_body_bytes: Maximum accepted request-body size in bytes. Disabled by default.
        require_json_content_type: Require a JSON media type for non-empty configured bodies.
        aggregate_validation_errors: Collect validation errors from every configured
            request source into one 422 response. Defaults to fail-fast behavior.

    Returns:
        A decorator that wraps the handler with validation logic.
    """
    if not isinstance(expose_internal_errors, bool):
        raise TypeError("expose_internal_errors must be a bool")
    if max_body_bytes is not None and (
        isinstance(max_body_bytes, bool)
        or not isinstance(max_body_bytes, int)
        or max_body_bytes <= 0
    ):
        raise ValueError("max_body_bytes must be a positive integer")
    if not isinstance(require_json_content_type, bool):
        raise TypeError("require_json_content_type must be a bool")
    if not isinstance(aggregate_validation_errors, bool):
        raise TypeError("aggregate_validation_errors must be a bool")

    if response_model is None and (
        not response_by_alias or response_exclude_none or response_exclude_unset
    ):
        raise ValueError("response serialization options require response_model")

    # Handle request_model shorthand
    if request_model is not None:
        warnings.warn(
            "The 'request_model' parameter of validate_http() is deprecated in favor "
            "of 'body' and will be removed in a future release. See "
            "https://github.com/yeongseon/azure-functions-validation-python/issues/223.",
            DeprecationWarning,
            stacklevel=2,
        )
        if any([body, query, path, headers]):
            raise ValueError("Cannot use request_model together with body/query/path/headers")
        body = request_model

    # Use default adapter if none provided
    if adapter is None:
        adapter = PydanticAdapter(legacy_loc=legacy_loc)

    def decorator(func: Callable[..., Any]) -> Callable[..., Any]:
        if _is_function_builder(func):
            raise RuntimeError(
                "@validate_http received an Azure Functions FunctionBuilder instead of "
                "your handler, which means it was applied ABOVE a binding decorator "
                "(e.g. @app.route). Validation is NOT active in this order and no "
                "endpoint metadata is emitted. Place @validate_http BELOW the binding "
                "decorator (innermost) so it wraps the handler directly."
            )

        # Cross-repo decorator-order guard (azure-functions-logging#310).
        if _has_logging_metadata(func):
            warnings.warn(
                "@validate_http is applied ABOVE @with_context (from "
                "azure-functions-logging). In this order, validation error "
                "responses (e.g. 4xx) are produced before @with_context runs, so "
                "they are logged WITHOUT correlation context. Place @with_context "
                "ABOVE @validate_http (outermost, just under @app.route) so it "
                "wraps the validated handler.",
                RuntimeWarning,
                stacklevel=2,
            )

        for option_name, model in (
            ("body", body),
            ("query", query),
            ("path", path),
            ("headers", headers),
            ("request_model", request_model),
            ("response_model", response_model),
        ):
            if isinstance(model, type(BaseModelV1)) and issubclass(model, BaseModelV1):
                raise TypeError(
                    f"{option_name} uses a Pydantic v1 model; Pydantic v2 models are required"
                )

        is_async = inspect.iscoroutinefunction(func)

        func_sig = inspect.signature(func)
        func_params = func_sig.parameters

        request_param_name = _find_request_param(func, func_params)
        _validate_no_conflicts(func, request_param_name, body, query, path, headers, request_model)

        # Pre-build TypeAdapter for response_model at decoration time (#97)
        response_type_adapter = TypeAdapter(response_model) if response_model is not None else None

        config = PipelineConfig(
            body=body,
            query=query,
            path=path,
            headers=headers,
            request_model=request_model,
            response_model=response_model,
            adapter=adapter,
            error_formatter=error_formatter,
            expose_internal_errors=expose_internal_errors,
            func_params=func_params,
            request_param_name=request_param_name,
            response_type_adapter=response_type_adapter,
            response_by_alias=response_by_alias,
            response_exclude_none=response_exclude_none,
            response_exclude_unset=response_exclude_unset,
            success_status_code=status_code,
            handler_name=getattr(func, "__qualname__", None) or getattr(func, "__name__", None),
            max_body_bytes=max_body_bytes,
            require_json_content_type=require_json_content_type,
            aggregate_validation_errors=aggregate_validation_errors,
        )

        wrapper = _make_wrapper(func, config, is_async=is_async)
        return wrapper

    return decorator
Response option Default Behavior
response_by_alias True Use serialization aliases in validated response JSON and endpoint response metadata.
response_exclude_none False Omit validated fields whose value is None.
response_exclude_unset False Omit validated fields that were not explicitly set.
max_body_bytes None Reject a configured body above this byte count with 413.
require_json_content_type False Require application/json or application/*+json for non-empty configured bodies; otherwise return 415.
aggregate_validation_errors False Collect normalized body/query/path/header validation failures into one 422; fail fast by default.

In aggregation mode, custom adapters continue to raise AdapterValidationError. The pipeline combines their normalized errors in source order and supplies an AggregatedValidationError to the configured formatter. Its errors attribute is the complete detail list. Malformed input and server-side failures are never aggregated.

The response serialization options require response_model. Non-default values without a response model raise ValueError at decoration time. They do not apply when a handler returns HttpResponse directly; that path remains a full bypass. Exclusion options alter payload presence, not the emitted response schema.

Body-policy failures are public typed exceptions:

  • PayloadTooLargeError maps to 413 with loc=["body"] and type payload_too_large.
  • UnsupportedMediaTypeError maps to 415 with loc=["headers", "content-type"] and type unsupported_media_type.

Custom formatters receive these original exceptions because both are 4xx errors. Endpoint metadata remains unchanged and does not yet advertise the optional 413/415 responses.

Usage example: body + response validation

import azure.functions as func
from pydantic import BaseModel

from azure_functions_validation import validate_http


class CreateInvoiceBody(BaseModel):
    customer_id: str
    amount: float


class CreateInvoiceResponse(BaseModel):
    invoice_id: str
    status: str


app = func.FunctionApp()


@app.function_name(name="create_invoice")
@app.route(route="invoices", methods=["POST"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(body=CreateInvoiceBody, response_model=CreateInvoiceResponse)
def create_invoice(req: func.HttpRequest, body: CreateInvoiceBody) -> CreateInvoiceResponse:
    return CreateInvoiceResponse(invoice_id="inv_1001", status="created")

Usage example: status codes and controlled errors

Use status_code= to set the success status (e.g. 201 for creation), and raise HttpError to return a controlled error through the standard {"detail": [...]} envelope without bypassing validation.

import azure.functions as func
from pydantic import BaseModel

from azure_functions_validation import HttpError, validate_http


class CreateUserBody(BaseModel):
    name: str


class UserResponse(BaseModel):
    id: int
    name: str


app = func.FunctionApp()

_USERS: dict[int, UserResponse] = {}


@app.function_name(name="create_user")
@app.route(route="users", methods=["POST"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(body=CreateUserBody, response_model=UserResponse, status_code=201)
def create_user(req: func.HttpRequest, body: CreateUserBody) -> UserResponse:
    user = UserResponse(id=len(_USERS) + 1, name=body.name)
    _USERS[user.id] = user
    return user  # HTTP 201


@app.function_name(name="get_user")
@app.route(route="users/{user_id}", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(response_model=UserResponse)
def get_user(req: func.HttpRequest) -> UserResponse:
    user = _USERS.get(int(req.route_params["user_id"]))
    if user is None:
        raise HttpError(404, "User not found")
    return user

Usage example: query + path + headers

import azure.functions as func
from pydantic import BaseModel, ConfigDict, Field

from azure_functions_validation import validate_http


class UserQuery(BaseModel):
    include_deleted: bool = False


class UserPath(BaseModel):
    user_id: int = Field(ge=1)


class UserHeaders(BaseModel):
    model_config = ConfigDict(populate_by_name=True)

    x_request_id: str = Field(alias="x-request-id")


app = func.FunctionApp()


@app.function_name(name="get_user")
@app.route(route="users/{user_id}", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(query=UserQuery, path=UserPath, headers=UserHeaders)
def get_user(
    req: func.HttpRequest,
    query: UserQuery,
    path: UserPath,
    headers: UserHeaders,
) -> dict[str, object]:
    return {
        "user_id": path.user_id,
        "include_deleted": query.include_deleted,
        "request_id": headers.x_request_id,
    }

Usage example: request_model shorthand (deprecated)

Deprecated

request_model is a deprecated alias for body and emits a DeprecationWarning. Use body= instead; it injects a parameter named body (rather than req_model). request_model will be removed in a future release. See the migration below.

import azure.functions as func
from pydantic import BaseModel

from azure_functions_validation import validate_http


class CreateTaskRequest(BaseModel):
    title: str


app = func.FunctionApp()


@app.function_name(name="create_task")
@app.route(route="tasks", methods=["POST"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(body=CreateTaskRequest)
def create_task(req: func.HttpRequest, body: CreateTaskRequest) -> dict[str, str]:
    return {"title": body.title}

Migrating from request_model

Replace @validate_http(request_model=Model) (injects req_model) with @validate_http(body=Model) (injects body).

Conflict rule

request_model cannot be combined with body, query, path, or headers. The decorator raises ValueError at import time if combined.

ResponseValidationError

Bases: Exception

Raised when response validation fails.

Initialize ResponseValidationError.

Parameters:

Name Type Description Default
message str

Error message

'Response validation error'
Source code in src/azure_functions_validation/errors.py
def __init__(self, message: str = "Response validation error"):
    """Initialize ResponseValidationError.

    Args:
        message: Error message
    """
    super().__init__(message)
    self.message = message

Usage example: handling response contract failures

import azure.functions as func
from pydantic import BaseModel

from azure_functions_validation import validate_http


class HealthResponse(BaseModel):
    status: str


app = func.FunctionApp()


@app.function_name(name="health")
@app.route(route="health", methods=["GET"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(response_model=HealthResponse)
def health(req: func.HttpRequest) -> dict[str, str]:
    # Returning an invalid shape to show failure behavior
    return {"state": "ok"}

When response validation fails, the runtime returns HTTP 500 with this payload:

{
  "detail": [
    {
      "loc": ["response"],
      "msg": "Response validation failed",
      "type": "response_validation_error"
    }
  ]
}

HttpResponse bypass

Returning azure.functions.HttpResponse directly bypasses response model validation by design.

ErrorFormatter

Usage example: custom validation error shape

import azure.functions as func
from pydantic import BaseModel

from azure_functions_validation import ErrorFormatter, validate_http


class InputModel(BaseModel):
    value: int


def app_error_formatter(exc: Exception, status_code: int) -> dict[str, object]:
    message = str(exc) if status_code < 500 else "Internal Server Error"
    return {
        "error": {
            "code": f"VALIDATION_{status_code}",
            "message": message,
        }
    }


formatter: ErrorFormatter = app_error_formatter

app = func.FunctionApp()


@app.function_name(name="custom_error")
@app.route(route="custom_error", methods=["POST"], auth_level=func.AuthLevel.ANONYMOUS)
@validate_http(body=InputModel, error_formatter=formatter)
def custom_error(req: func.HttpRequest, body: InputModel) -> dict[str, int]:
    return {"value": body.value}

Formatter signature

Keep the formatter signature exactly (exc: Exception, status_code: int) -> dict[str, Any].

For 4xx responses, the formatter receives the original exception. For 5xx responses, it receives InternalServerError("Internal Server Error") by default, while the original is logged server-side with traceback. The unsafe expose_internal_errors=True option restores original-exception passthrough.

HttpError

Bases: Exception

Raised by a handler to return a controlled HTTP error response.

Rendered through the standard error envelope ({"detail": [...]}) by the validation pipeline, so controlled errors (e.g. 404, 409) share the same shape as automatic validation errors.

Parameters:

Name Type Description Default
status_code int

HTTP status code for the response (e.g. 404).

required
detail Any

Either a human-readable message (wrapped into a single {"loc": [], "msg": ..., "type": ...} entry) or a pre-built list of detail mappings matching the error-envelope schema.

'Error'
error_type str

type value used when detail is a plain message.

'http_error'
Source code in src/azure_functions_validation/errors.py
def __init__(
    self,
    status_code: int,
    detail: Any = "Error",
    *,
    error_type: str = "http_error",
) -> None:
    super().__init__(str(detail))
    self.status_code = status_code
    self.detail = detail
    self.error_type = error_type

to_detail()

Return the error-envelope detail list for this error.

Source code in src/azure_functions_validation/errors.py
def to_detail(self) -> list[dict[str, Any]]:
    """Return the error-envelope ``detail`` list for this error."""
    if isinstance(self.detail, list):
        return self.detail
    return [{"loc": [], "msg": str(self.detail), "type": self.error_type}]

Raise HttpError(status_code, detail) from a handler to return a controlled HTTP error rendered through the standard {"detail": [...]} envelope. detail may be a plain message (wrapped into a single entry) or a pre-built list of {"loc", "msg", "type"} mappings. Errors with status_code >= 500 are sanitized so internal details never leak to clients.

azure_functions_validation.testing.MockHttpRequest

A public test helper for unit-testing validated handlers. It subclasses the real azure.functions.HttpRequest, so it drives the genuine @validate_http pipeline end-to-end without a running Functions host.

from azure_functions_validation.testing import MockHttpRequest

request = MockHttpRequest(
    method="POST",
    json={"name": "Alice", "email": "alice@example.com"},
    params={"debug": "true"},
)
response = create_user(request)
assert response.status_code == 200

See Testing for the full list of constructor options.

Error response shape reference

Default validation and parsing errors use this envelope:

{
  "detail": [
    {
      "loc": ["body", "field_name"],
      "msg": "Field required",
      "type": "missing"
    }
  ],
  "error_format_version": 1
}

Stability contract

Every default error envelope carries a top-level error_format_version integer. It lets downstream consumers (frontends, API gateways) pin against a known schema and detect breaking changes explicitly instead of silently misparsing new fields.

  • The current version is 1.
  • The integer is bumped only when the default envelope changes in a backwards-incompatible way; additive, optional fields do not bump it.
  • The marker is present on all built-in envelopes — validation errors (4xx), sanitized server errors (5xx), and the internal-failure fallback.
  • A custom ErrorFormatter owns its output shape entirely: the marker is never injected into a successful custom formatter's response. Emit your own version field there if you need one.

Common status codes:

  • 400: an adapter raises MalformedRequestError for syntactically malformed client input, including invalid JSON or UTF-8 ("Invalid JSON").
  • 422: request validation failed.
  • 500: request binding failure, response validation failure, or any unexpected adapter exception, including a plain ValueError.

Typical loc values

  • body errors: loc starts with "body"
  • query errors: loc starts with "query"
  • path errors: loc starts with "path"
  • header errors: loc starts with "headers"
  • response errors: loc equals ["response"]

Opting out of the source prefix

The leading source segment (body / query / path / headers) was added to disambiguate same-named fields across inputs. To keep the previous unprefixed loc for one migration cycle, pass legacy_loc=True to validate_http. This escape hatch will be removed in a future release.

Custom adapters

ValidationAdapter (the adapter protocol) and PydanticAdapter (the default implementation) are part of the public API. Pass a custom adapter= to validate_http to plug in a non-Pydantic validation backend; most deployments should keep the default PydanticAdapter.

See Writing a custom adapter for every method's input, output, exception and content-type requirements, instance-reuse/thread-safety rules, formatter ordering, protocol versioning policy, and conformance-suite usage.

Each parse_body, parse_query, parse_path, and parse_headers method must raise MalformedRequestError only for syntactically malformed client input (400) and AdapterValidationError for validation failures (422). Every other exception is treated as an adapter or server fault, logged with traceback, and returned as a sanitized 500. This is intentionally breaking: a custom adapter's plain ValueError is no longer classified as a client error.

from azure_functions_validation import PydanticAdapter, ValidationAdapter, validate_http

adapter: ValidationAdapter = PydanticAdapter(legacy_loc=False)


@validate_http(body=RequestModel, adapter=adapter)
def handler(req, body): ...

Internal references

These modules are useful for advanced extension work but are internal APIs:

  • pipeline.py: PipelineConfig, run_pipeline, run_pipeline_async

For full implementation patterns, see Usage and Architecture.