A Python package for simulating Active Inference agents in Markov Decision Process environments. Please see our companion paper, published in the Journal of Open Source Software: "pymdp: A Python library for active inference in discrete state spaces" for an overview of the package and its motivation. For a more in-depth, tutorial-style introduction to the package and a mathematical overview of active inference in Markov Decision Processes, see the longer arxiv version of the paper.
Fork notice. This checkout is the Active Inference Institute fork (
ActiveInferenceInstitute/pymdp, default branchmaster) ofinfer-actively/pymdp. Fork-local additions stay additive; seeAGENTS.mdandTODO.md.
- Project status
- Documentation
- Citing
pymdp - Quick-start: Installation and Usage
- Getting started / introductory material
- Publishing models as Generalized Notation Notation (GNN)
- Contributing
- Contributors
pymdp is released and actively developed. The default backend since 1.0.0 is
JAX; the archived NumPy implementation lives under pymdp/legacy/ for
migration and cross-validation. Install the unit-test dependencies with
uv sync --group test.
- Gates: one entry point,
./gate.sh <arm>; the unit gate is./gate.sh test(nightly tests and the notebook suite are separate arms). Arms are listed indocs-mkdocs/development/testing.md. - Docs:
docs/is the fork's navigation hub. Install the docs extra withuv sync --group test --extra docs, then run./gate.sh docsto sync notebooks and build the local site fromdocs-mkdocs/. - Dependencies: a fresh install downloads public dependencies. The unit tests and docs build run locally once the dependencies are installed.
- GNN interop:
./gate.sh testcovers emission and refusal behavior../gate.sh gnnrequires the pinned GNN source for parser and execution checks.
- Install, run a JAX example, migrate from NumPy, or export a GNN model. Guides
- Look up array shapes, agent and environment interfaces, or the packaged API. Reference
- Build this checkout's site, run the gates, or maintain the fork. Development
- Render the JOSS source edition and verify its provenance. Manuscript
The published upstream site follows the upstream release. The fork's GNN bridge and other checkout-specific additions are documented in this repository and its local MkDocs build. Upstream documentation
If you use pymdp in your work or research, please cite:
@article{Heins2022,
doi = {10.21105/joss.04098},
url = {https://doi.org/10.21105/joss.04098},
year = {2022},
publisher = {The Open Journal},
volume = {7},
number = {73},
pages = {4098},
author = {Conor Heins and Beren Millidge and Daphne Demekas and Brennan Klein and Karl Friston and Iain D. Couzin and Alexander Tschantz},
title = {pymdp: A Python library for active inference in discrete state spaces},
journal = {Journal of Open Source Software}
}
This package is hosted on the infer-actively GitHub organization, which was built with the intention of hosting open-source active inference and free-energy-principle related software.
Most of the low-level mathematical operations are NumPy ports of their equivalent functions from the SPM implementation in MATLAB. We have benchmarked and validated most of these functions against their SPM counterparts.
Here's a visualization of pymdp agents in action. One of the defining features of active inference agents is the drive to maximize "epistemic value" (i.e. curiosity). Equipped with such a drive in environments with uncertain yet disclosable hidden structure, active inference can ultimately allow agents to simultaneously learn about the environment as well as maximize reward.
The simulation below (see associated JAX notebook here) demonstrates what might be called "epistemic chaining," where an agent (here, analogized to a mouse seeking food) forages for a chain of cues, each of which discloses the location of the subsequent cue in the chain. The final cue (here, "Cue 2") reveals the location a hidden reward. This is similar in spirit to "behavior chaining" used in operant conditioning, except that here, each successive action in the behavioral sequence doesn't need to be learned through instrumental conditioning. Rather, active inference agents will naturally forage the sequence of cues based on an intrinsic desire to disclose information. This ultimately leads the agent to the hidden reward source in the fewest number of moves as possible.
You can run the code behind simulating tasks like this one and others in the Examples section of the official documentation. The GIF generation script used for these animations is available here.
We recommend installing pymdp using uv, with an explicit virtual environment:
uv venv .venv
source .venv/bin/activate
uv pip install inferactively-pymdpIf you prefer pip, use:
pip install inferactively-pymdpIf uv sync --group test or uv sync --extra nb fails while building pygraphviz, install Graphviz first and then retry. On macOS, install graphviz with Homebrew. On Ubuntu/Debian, install graphviz libgraphviz-dev pkg-config build-essential python3-dev (or the version-matched python3.x-dev package if needed). The full troubleshooting notes live in docs-mkdocs/getting-started/installation.md.
Once in Python, you can then directly import pymdp, its sub-packages, and functions.
from jax import numpy as jnp, random as jr
from pymdp import utils
from pymdp.agent import Agent
key = jr.PRNGKey(0)
keys = jr.split(key, 3)
num_obs = [3, 5]
num_states = [3, 2]
num_controls = [3, 1]
A = utils.random_A_array(keys[0], num_obs, num_states)
B = utils.random_B_array(keys[1], num_states, num_controls)
C = utils.list_array_uniform([[no] for no in num_obs])
agent = Agent(A=A, B=B, C=C, batch_size=1)
observation = [jnp.array([1]), jnp.array([4])]
qs, info = agent.infer_states(observation, empirical_prior=agent.D, return_info=True)
# Optional diagnostic: current variational free energy for each batch element.
vfe = info["vfe"]
q_pi, neg_efe = agent.infer_policies(qs)
action_keys = jr.split(keys[2], agent.batch_size + 1)
action = agent.sample_action(q_pi, rng_key=action_keys[1:])We recommend starting with the JAX-first official documentation for the repository, which provides practical guides, curated notebooks, and generated API references.
For new users to pymdp, we specifically recommend stepping through following three Jupyter notebooks (can also be used on Google Colab):
We also have (and are continuing to build) a series of notebooks that walk through active inference agents performing different types of tasks in the Notebook Gallery.
This fork's pymdp.gnn bridge exports flat discrete POMDPs with one observation
modality and one hidden-state factor as Generalized Notation Notation
(GNN) specifications. The interoperability guide
shows how to install the fork from source, export a three-state example,
and validate it against the GNN v5.4.0 parser recorded in .github/gnn-pin.json.
GNN can then render and execute the specification through its pymdp route.
See GNN's downstream adoption guide
for integration guidance.
Other repositories that specify or consume models in GNN:
active_inference_battleship— a playable model specified in GNN and compiled at build time.active_inference_language_model— a language-model stack with GNN specifications and conformance checks.ActiveBlockferenceandActInf_RxInfer— block-based and RxInfer implementations in the same ecosystem.
This package is under active development. If you would like to contribute, please refer to CONTRIBUTING.md.
Recommended local setup:
cd <path_to_repo_fork>
uv venv .venv
source .venv/bin/activate
uv sync --group testRecommended contributor hooks:
uv sync --group dev
uv run --group dev pre-commit installUseful variants:
# tests + contributor hooks
uv sync --group test --group dev
# docs work
uv sync --group test --extra docs
# notebook/media extras
uv sync --group test --extra nb
# model fitting extras
uv sync --group test --extra modelfitRun the gates:
./gate.sh test # unit gate (nightly tests deselected)
./gate.sh docs # strict mkdocs build
./gate.sh all # everything except the nightly tierEvery arm is documented in AGENTS.md and
docs-mkdocs/development/testing.md;
measured arm times live in the generated
docs-mkdocs/development/gate-metrics.md.
- Conor Heins @conorheins
- Tim Verbelen @tverbele
- Dimitrije Markovic @dimarkov
- Riddhi Jain Pitliya @riddhipits
- Arun Niranjan @Arun-Niranjan
- Toon Van de Maele @toonvdm
- Ozan Catal @OzanCatalVerses
- Tommaso Salvatori @salvatomm
- Aswin Paul @aswinpaul
- Lancelot Da Costa @lancelotdacosta
- Ran Wei @ran-weii
- Alexander Tschantz @alec-tschantz
- Miguel de Prado @praesc
- Nikola Pižurica @NIkolaPizurica
- Nikola Milović @nikolamilovic-ft
- Matteo Risso @matteorisso
- Christopher Buckley @clb27
- Beren Millidge @BerenMillidge
- Daphne Demekas @daphnedemekas
- Cooper Williams @coopwilliams
This repository carries a committed .aii/ sidecar with curated
InstituteOS metadata (aii-sidecar/v1): category, maturity, ecosystem relations,
key artifacts, provenance, and portable tasks. It is read by
InstituteOS tooling and
does not affect the project's build or runtime.

