Skip to main content

Easily generate type-safe and async Python applications from AsyncAPI 3 specifications.

Project description

AsyncAPI Python Code Generator

[!IMPORTANT] Although commits to dev branch might seem infrequent, the project is under active development as of June 2025.

We currently produce only those changes that are required to satisfy our personal use cases.

The number of commits will grow as we see increase in popularity of it, so do not hesitate to star this repo, open issues, and pull requests.

Link to this github repository

Easily generate type-safe and async Python applications from AsyncAPI 3 specifications.

Features

  • Creates Application class from AsyncAPI 3 specifications, implementing every operation in the file
  • Generates typed Python code (messages are generated using datamodel-code-generator)
  • Performs dynamic validation of messages with Pydantic 2
  • Enforces the user code to implement all consumer methods as described by spec
  • Provides async code
  • Spec parser references other files through absolute or relative paths
  • Spec parser references other files through url
  • Supports request-reply pattern
  • Supports publish-subscribe pattern
  • AsyncAPI trait support
  • Customizable message encoder/decoder
  • Works as a plugin for pantsbuild (see instructions below)

Requirements

  • python>=3.9
  • pydantic>=2
  • pytz
  • For codegen extra
    • jinja2
    • typer
    • datamodel-code-generator
    • pyyaml
  • For amqp extra
    • aio-pika

Installation

For code generation (development env), run:

pip install asyncapi-python[codegen]

For runtime, run:

pip install asyncapi-python[amqp]

You can replace amqp with any other supported protocols. For more info, see Supported Protocols section.

Usage as a Pants plugin

The following method was tested with pants version 2.23.1. Pleas note that Pants plugin API is still in development and things might break.

This library can act as a plugin for Pants. More specifically, it creates a new target type: asyncapi_python_service -- which can be used like:

# BUILD
asyncapi_python_service(
  name="asyncapi_app",
  service="app.asyncapi.yaml",
  sources=[
    "app.asyncapi.yaml", 
    "lib.asyncapi.yaml", 
    "commons.*.asyncapi.yaml"
  ]
)

This will be generating python module named asyncapi_app on codegen-export, and export goals. This target can later be used as a dependency of python_sources.

# BUILD
python_sources(
  dependencies=[
    ":asyncapi_app",
    ":reqs",
  ],
)

python_requirements(
  name="reqs",
)

Note that this plugin does not do dependency injection, so asyncapi-python must be a dependency

# requirements.txt
asyncapi-python[amqp]

Deploying this plugin into your pants monorepo

# pants.toml
plugins = [
  "asyncapi_python[codegen]==0.2.5",  # Plugin version MUST match the version of your python clients
  ...
]
backend_packages = [
    "asyncapi_python_pants",
    ...
]
pythonpath = ["%(buildroot)s/pants-plugins"]

When everything is done, test if the plugin works by running pants export-codegen ::

Supported Protocols / Use Cases

Below, you may see the table of protocols and the supported use cases. The tick signs (✅) contain links to the examples for each implemented protocol-use case pair, while the hammer signs (🔨) contain links to the Issues tracking the progress for protocol-use case pairs. The list of protocols and use cases is expected to increase over the progress of development.

Use Case AMQP
Pub-Sub ✅ amqp-pub-sub
Work Queue ✅ amqp-work-queue
RPC ✅ amqp-rpc

Documentation

Although there's no documentation available at the moment, a set of comprehensive examples with comments for each implemented use-case is under the examples directory.

Contributing

Contributions are welcome! Please feel free to open an Issue or submit a Pull Request.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncapi_python-0.2.5.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

asyncapi_python-0.2.5-py3-none-any.whl (40.8 kB view details)

Uploaded Python 3

File details

Details for the file asyncapi_python-0.2.5.tar.gz.

File metadata

  • Download URL: asyncapi_python-0.2.5.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for asyncapi_python-0.2.5.tar.gz
Algorithm Hash digest
SHA256 8adde1d9f9afcb3bac13aaac3a998742dd7a3083d26cb2196a24c9ea9e467b53
MD5 776ee1106bb04325ce368860cadc455c
BLAKE2b-256 f23a6a69a54ef93c75afcf629fbd7fde1ac8e0c89a75c2bc737961bdb2b29938

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncapi_python-0.2.5.tar.gz:

Publisher: release.yml on G-USI/asyncapi-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asyncapi_python-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncapi_python-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 8212aadea4366bd3f44c2a4a8a95cf10762b5862e712ca0014752c408cf7287c
MD5 cb992c7906dbe39cd43ee739ee8f1a0b
BLAKE2b-256 f7b4085232377abd184f4c9107c6f745b6bc4dbe219e7993db1442551922dfa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncapi_python-0.2.5-py3-none-any.whl:

Publisher: release.yml on G-USI/asyncapi-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page