{"id":4204,"library":"pyro-api","title":"Generic API for Pyro Backends","description":"pyro-api provides a generic, backend-agnostic API for probabilistic programming, allowing users to write code that can be executed with different Pyro backends (e.g., Pyro, NumPyro, Funsor) without modification. It aims to abstract common functionalities like distributions, inference primitives, and parameter management. The current version is 0.1.2, and it maintains a slow release cadence, primarily focusing on stability and compatibility.","status":"active","version":"0.1.2","language":"en","source_language":"en","source_url":"https://github.com/pyro-ppl/pyro-api","tags":["machine learning","probabilistic programming","interface","backend","pyro"],"install":[{"cmd":"pip install pyro-api","lang":"bash","label":"Install pyro-api"}],"dependencies":[],"imports":[{"note":"The pyro.generic module was moved to the standalone pyroapi package in version 0.1.0.","wrong":"import pyro.generic","symbol":"pyroapi","correct":"import pyroapi"},{"symbol":"set_backend","correct":"from pyroapi import set_backend"},{"symbol":"distributions","correct":"import pyroapi.distributions as dist"},{"symbol":"param","correct":"import pyroapi.params as params"},{"symbol":"handlers","correct":"import pyroapi.handlers as handlers"}],"quickstart":{"code":"import pyroapi\nimport os\n\n# IMPORTANT: pyro-api is an interface. You must install a backend.\n# For example, run: pip install pyro\n# Or for NumPyro: pip install numpyro\n\n# Set the backend. This line typically goes near the start of your script.\n# 'pyro' is a common default, but 'numpyro' or 'funsor' are also options.\n# If the chosen backend is not installed, set_backend will raise an error.\ntry:\n    backend_name = os.environ.get('PYRO_BACKEND', 'pyro') # Use env var for testing\n    pyroapi.set_backend(backend_name)\n    print(f\"Pyro-API backend successfully set to: {pyroapi.get_backend()}\")\n\n    # Now you can use the generic API for distributions, parameters, etc.\n    import pyroapi.distributions as dist\n    import pyroapi.params as params\n\n    # Example: Define a generic parameter and a distribution\n    mean_param = params.param(\"mean_value\", 0.0)\n    std_param = params.param(\"std_value\", 1.0, constraint=dist.constraints.positive)\n\n    generic_normal = dist.Normal(mean_param, std_param)\n    print(f\"\\nCreated a generic Normal distribution with mean {mean_param.data} and std {std_param.data}\")\n\nexcept ImportError as e:\n    print(f\"Failed to set Pyro-API backend: {e}\")\n    print(\"Please ensure your chosen backend (e.g., 'pyro' or 'numpyro') is installed.\")\nexcept Exception as e:\n    print(f\"An unexpected error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to set a backend for `pyro-api` and access its generic modules for distributions and parameters. Remember, `pyro-api` is an interface; you must install a concrete backend like `pyro` or `numpyro` for it to function correctly. The `set_backend()` call typically needs to happen early in your application's lifecycle."},"warnings":[{"fix":"Update imports to use `pyroapi` directly, e.g., `import pyroapi`, `from pyroapi import set_backend`, `import pyroapi.distributions as dist`.","message":"The `pyro.generic` module was moved out of the `pyro` package into the standalone `pyro-api` package in version 0.1.0. Direct imports like `from pyro.generic import something` will fail.","severity":"breaking","affected_versions":">=0.1.0"},{"fix":"Run `pip install <your-chosen-backend>` (e.g., `pip install pyro`) and include `pyroapi.set_backend(\"pyro\")` early in your application's setup.","message":"`pyro-api` is an interface and does not provide an implementation itself. You must install a backend library (e.g., `pyro`, `numpyro`, `funsor`) and call `pyroapi.set_backend()` before using any generic API features.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Strive for full generic API usage when writing portable code. For example, use `pyroapi.tensor(data)` instead of `torch.tensor(data)`.","message":"When using `pyro-api`, ensure that all operations are performed through the generic API (e.g., `pyroapi.distributions`, `pyroapi.params`). Mixing generic API calls with direct backend-specific calls (e.g., `torch.tensor` directly when using `pyro` backend) can lead to unexpected behavior or break backend portability.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}