Skip to content

Repository files navigation

OpenAPI Parser

PyPI - VersionPyPI - DownloadsPyPI - Python VersionPyPI - Format

Parse OpenAPI 3 documents into fully typed Python dataclass objects. Navigate your API specification programmatically — servers, paths, operations, parameters, schemas, security schemes, and more.

VersionStatus
2.0Deprecated
3.0Supported
3.1In development

Installation

pip install openapi3-parser

Quick Start

fromopenapi_parserimportparsespecification=parse("swagger.yml")
print(specification.info.title) # e.g. "User example service"

Use Cases

Parse from different sources

# From file pathspec=parse("specs/openapi.yml")
# From URLspec=parse("https://example.com/openapi.json")
# From raw stringspec=parse(spec_string="""openapi: "3.0.0"info: title: My API version: "1.0.0"paths: {}""")

Navigate servers, paths, and operations

specification=parse("swagger.yml")
# List all serversforserverinspecification.servers:
print(f"{server.description} - {server.url}")
# List all paths and their HTTP methodsforpathinspecification.paths:
methods=", ".join(op.method.valueforopinpath.operations)
print(f"{path.url}: [{methods}]")
# Inspect operation detailsforpathinspecification.paths:
foropinpath.operations:
print(f"[{op.method.value}] {path.url}: {op.summary}")
ifop.deprecated:
print(" (deprecated)")
ifop.operation_id:
print(f" operationId: {op.operation_id}")

Enum strictness

By default, content types, string formats, and other enum fields are validated against predefined enums. For specs that use custom values, pass strict_enum=False:

# Accepts non-standard content types like "application/vnd.api+json"spec=parse("swagger.yml", strict_enum=False)

When strict mode is off, unrecognized values are wrapped in a LooseEnum object instead of raising an error.

Error Handling

fromopenapi_parser.errorsimportParserErrortry:
spec=parse("invalid.yml")
exceptParserErrorase:
print(f"Parsing failed: {e}")

Data Model

Parsed documents return a Specification object composed of fully typed dataclasses:

ModelDescription
SpecificationRoot document — version, info, servers, paths, schemas, security
InfoAPI metadata — title, version, description, contact, license
ServerServer definition — url, description, variables
PathURL path — operations, parameters
OperationHTTP method — responses, parameters, request body, security
ParameterPath/query/header/cookie param — schema, style, required
ResponseStatus code, description, content, headers
RequestBodyContent, description, required
ContentMedia type, schema, example
SchemaBase type — Integer, Number, String, Boolean, Array, Object, Null
PropertyObject property — name, schema
OneOf/AnyOfComposition schemas with discriminator support
SecuritySecurity scheme — apiKey, http, oauth2, openIdConnect
OAuthFlowOAuth flow — authorization, token, scopes
HeaderResponse header — name, schema, description
TagTag with optional external docs
ExternalDocExternal documentation reference
DiscriminatorPolymorphism discriminator — property name, mapping

See the specification module for all available fields and types.

Development

# Install with dev dependencies
uv sync --dev
# Lint
uv run ruff check .
uv run mypy .
uv run ty check .# Test
uv run pytest
# Format
uv run ruff format .

About

OpenAPI 3 parser to use a specification inside of the code in your projects

Topics

Resources

Security policy

Stars

86 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages