Registry / serialization / apispec

apispec

JSON →
library6.10.0pypypi✓ verified 52d ago

APISpec is a pluggable Python library designed for generating API specifications. It primarily supports the OpenAPI Specification (formerly known as the Swagger specification), enabling developers to programmatically define their API's structure, endpoints, and data models. It is framework-agnostic and offers built-in integration capabilities, notably with Marshmallow. The library maintains an active development status, with frequent patch and minor releases, and new major versions typically released on an annual or bi-annual cadence.

serializationweb-frameworkhttp-networking
pip install apispec
Install & Compatibility
Where this runs
tested against v6.10.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.990 runs
installs and imports cleanly · install 0.0s · import 0.033s · 20.9MB
glibc
py 3.103.990 runs
installs and imports cleanly · install 1.7s · import 0.028s · 22MB
19MB installed
● package 19MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

APISpec
from apispec import APISpec
MarshmallowPlugin
from apispec.ext.marshmallow import MarshmallowPlugin
FlaskPlugin
from apispec_webframeworks.flask import FlaskPlugin
from apispec.ext.flask import FlaskPlugin
Web framework plugins like FlaskPlugin were moved to the `apispec-webframeworks` package in APISpec 1.0.0 and later.

This quickstart demonstrates how to initialize an `APISpec` object, define a Marshmallow schema, register it as an OpenAPI component, and then add a path with an operation that references the defined schema. Finally, it prints the generated OpenAPI specification in JSON format. This illustrates the basic programmatic API definition workflow using `apispec` and its Marshmallow plugin.

from apispec import APISpec from apispec.ext.marshmallow import MarshmallowPlugin from marshmallow import Schema, fields import json # 1. Create an APISpec object spec = APISpec( title="My Awesome API", version="1.0.0", openapi_version="3.0.2", info=dict(description="A minimal example of APISpec"), plugins=[ MarshmallowPlugin(), ], ) # 2. Define a Marshmallow Schema and register it as an OpenAPI component class UserSchema(Schema): id = fields.Int(dump_only=True) name = fields.Str(required=True, description="The user's name") email = fields.Email(required=True, description="The user's email address") spec.components.schema("User", schema=UserSchema) # 3. Add a path with operations referencing the schema spec.path( path="/users/{user_id}", operations=dict( get=dict( summary="Get user by ID", parameters=[ { "in": "path", "name": "user_id", "schema": {"type": "integer"}, "required": True, "description": "Numeric ID of the user to get", } ], responses={ 200: { "description": "User data", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/User"}}}, } }, ) ), ) # 4. Output the spec as JSON print(json.dumps(spec.to_dict(), indent=2))
Debug
Known issues
breakingThe `openapi_version` parameter in `APISpec` constructor is no longer optional and must be explicitly provided. It previously defaulted to '2.0'.
fix
Always pass `openapi_version='2.0'` or `openapi_version='3.0.2'` (or another desired version) when initializing `APISpec`.
affects: >=1.0.0
breakingWeb framework integration plugins (e.g., for Flask, Bottle, Tornado) were moved from `apispec.ext` to a separate package, `apispec-webframeworks`.
fix
Install `apispec-webframeworks` (`pip install apispec-webframeworks`) and update your import statements (e.g., `from apispec_webframeworks.flask import FlaskPlugin`).
affects: >=1.0.0
breakingWhen referencing components (schemas, parameters, etc.), `apispec` now strictly requires referencing them by their ID, not their full path (e.g., use `'User'` instead of `#/definitions/User` or `#/components/schemas/User`).
fix
Ensure all internal references to components within your spec are by their short ID. `apispec` will generate the correct full path based on the OpenAPI version.
affects: >=2.0.0
breakingSupport for Marshmallow 2.x was dropped. `apispec` now requires Marshmallow 3.13.0 or newer when using the Marshmallow plugin. This includes significant API changes in Marshmallow 3.x/4.x, such as the removal of the `description` parameter from field constructors.
fix
Upgrade your Marshmallow installation to a compatible version (e.g., `pip install -U 'marshmallow>=3.13.0'`). Consider using `apispec[marshmallow]` to ensure compatible versions are installed. Additionally, update your Marshmallow schemas to use syntax compatible with Marshmallow 3.x/4.x (e.g., replace `fields.Str(description=...)` with `fields.Str(metadata={'description': '...'})` and similar adjustments for other removed arguments like `allow_none`).
affects: >=4.0.0
gotchaIf you are creating custom `apispec` plugins, their helper methods must accept `**kwargs` in their signature, as `APISpec.path` may pass additional arguments.
fix
Modify custom plugin helper method signatures to include `**kwargs` (e.g., `def my_helper(self, obj, **kwargs):`).
affects: >=2.0.0
gotchaThe `extra_fields` parameter for adding additional fields to schemas was removed. All fields should now be passed directly within the component dictionary.
fix
Instead of `spec.components.schema('MySchema', schema=MySchema, extra_fields={'new_field': {'type': 'string'}}),` directly include all fields in your Marshmallow Schema or the component dictionary passed to `spec.components.schema`.
affects: >=1.0.0
breakingMarshmallow 4.x introduced breaking changes to `marshmallow.fields`, notably removing direct `description` and `example` arguments from field constructors. If `apispec`'s dependency range (e.g., `marshmallow>=3.18.0` for `apispec>=6.0.0`) installs Marshmallow 4.x, your schemas written for Marshmallow 3.x will raise a `TypeError` for these arguments.
fix
Update your Marshmallow schema definitions to be compatible with Marshmallow 4.x. For example, change `fields.Str(description='...')` to `fields.Str(metadata={'description': '...'})` and `fields.Str(example='...')` to `fields.Str(metadata={'example': '...'})`. Alternatively, pin your Marshmallow version to `<4.0.0` (e.g., `marshmallow>=3.18.0,<4.0.0`) if you are unable to update your schema definitions.
affects: >=6.0.0
Errors
Common errors & fixes
openapi_spec_validator.exceptions.OpenAPIValidationError: 'responses' is a required property
The OpenAPI specification is missing the 'responses' field in the operation definition.
fix
Ensure that each operation in your OpenAPI specification includes a 'responses' field with appropriate response definitions.
apispec.exceptions.DuplicateComponentNameError: Component name 'ExampleComponent' is already registered
Attempting to register a component with a name that has already been used in the specification.
fix
Use unique names for each component when registering them in the specification.
apispec.exceptions.InvalidParameterError: Parameter must contain required keys: 'name', 'in', 'schema'
A parameter definition is missing one or more required keys: 'name', 'in', or 'schema'.
fix
Ensure that all parameter definitions include the 'name', 'in', and 'schema' keys with appropriate values.
apispec.exceptions.PluginMethodNotImplementedError: Plugin method 'example_method' not implemented
A plugin method is being called that has not been implemented in the plugin.
fix
Implement the required method in your plugin or avoid calling unimplemented methods.
apispec.exceptions.OpenAPIError: OpenAPI spec validation failed
The generated OpenAPI specification does not conform to the OpenAPI standard.
fix
Validate your OpenAPI specification against the OpenAPI standard and correct any validation errors.
Upgrade
Version history
6.10.0latest on PyPI
Audit
Dependencies
pythonrequiredRequired Python version
packagingrequiredDependency for core functionality
marshmallowoptionalRequired for apispec.ext.marshmallow plugin
pyyamloptionalRequired for YAML serialization (apispec[yaml] extra)
Agent activity
79 hits · last 30 days
node
12
seranking-bot
4
ahrefsbot
3
bytedance
2
Amazon
1
amazonbot
1
Resources