Skip to content

Code conventions

The rule book of the server is AGENTS.md. It is written for AI assistants and holds for people too. This page lists the points a review checks most often. Several of them are also pre-commit hooks, see Test your change.

  • Async only. The server is one event loop, so a blocking call stalls everything. Use an async library, or asyncio.to_thread for blocking work. The check_blocking_io hook catches known blocking calls. New to asyncio? Start with the asyncio documentation.
  • Names that explain. A function, variable or class name says what it does or holds, so a reader does not have to guess.
  • Private methods at the bottom. Public methods come first, private ones last; the check_method_order hook checks it within a class.
  • Reuse the helpers. Look in music_assistant/helpers/ before you write a utility. For the current time, use utc() or now() from helpers/datetime.py, never datetime.now().

A docstring tells the caller what a function does and what its parameters mean, not how it works inside. Use Sphinx style with :param:, and start a multi-line docstring on the line after the quotes:

async def get_report(self, include_log_tail: bool = False) -> dict[str, Any]:
"""
Return a full (sanitized) diagnostics report.
:param include_log_tail: Include the recent warning/error log excerpt.
"""

Write a comment only where the code is not clear on its own. Keep the comments others wrote unless they no longer hold. Docstrings and comments describe the code as it is, not what changed.

The README.md in the folder of a controller or provider explains its layout, data model and design. A change that makes it wrong or incomplete updates it in the same pull request.

A change to a config key or a database table needs a migration, and a maintainer decides how before the work is done. Where data lives explains the rules.

A new command, a new or changed parameter, or a new field the server sends raises API_SCHEMA_VERSION in music_assistant/constants.py. The comment next to it says when, and The API and events explains why.

Text a user sees lives in a strings.json: the shared one in music_assistant/ or the one in the folder of your provider or controller. Code refers to it by key:

  • A ConfigEntry has no label in code. Its label and description are config_entries.<key>.label and config_entries.<key>.description in strings.json.
  • A browse or recommendation folder sets translation_key, with its English name in strings.json.

scripts/build_translations.py combines every strings.json into translations/en.json, which the translators work from. The check_config_entries and check_translatable_labels hooks fail on text written in code.

Music Assistant streams music to your own speakers; it is not a way to download or keep copies of it. Some guards exist only for that reason and must stay: the read-rate pacing on the stream endpoints, background audio analysis only on local files, and the artist tracks listing that stays empty for a provider without an artist tracks endpoint. The usage policy and Streaming rules explain what a provider must and must not do.

Music Assistant is a project of the Open Home Foundation.