Python API
Import pymalloy as pm. The root namespace exposes constructors, native runtime owners, language tools, the widget, and the common error base.
import pymalloy as pm
with pm.connect() as con:
result = con.model("run: duckdb.sql('SELECT 42 AS answer') -> { select: answer }").query().run()
print(result.to_pylist())The result is [{'answer': 42}]. Malloy compiles the source and DuckDB, an embedded SQL database, runs it.
| Root names | Contract |
|---|---|
table, sql, data, source, view | Source and View construction |
col, lit, given, count, case, Expr, Sort | Scalar expressions |
draft, read_model, Source, View, Draft | Draft composition and editing |
connect, Connection, Model, Query, Result, ModelSource | Runtime and result contracts |
parse, format | Language tools |
MalloyWidget | Widget inputs, state, and browser assets |
PyMalloyError | Domain errors |
Native wheels include the compiler, DuckDB execution, widgets, and installed agent guidance. PyMalloy installs PyArrow for data capture and columnar results. Install Polars, a dataframe library, separately for Result.to_polars().
Browser Python uses asynchronous pymalloy.browser connections to run the same Malloy compiler with DuckDB WebAssembly. Install the browser wheel in Pyodide, a browser-hosted Python runtime. The browser Python guide covers setup, widgets, and queries.
Qualified modules
| Module | Additional public values |
|---|---|
pymalloy.authoring | Input, source/view/draft constructors |
pymalloy.expressions | call, number, scalar constructors |
pymalloy.analysis | Reports, schemas, locations, and to_dict |
pymalloy.errors | Specific exceptions and ExecutionContext |
pymalloy.validation | DocumentationPolicy, Validation, DataCheck |
pymalloy.result | Result, Column |
pymalloy.widget | MalloyWidget |
pymalloy.browser | Asynchronous connections, models, queries, results, and runtime assets |
pymalloy.export | Notebook preparation, renderers, and bundles |
pymalloy.agent | agent_plugin(), agent_skill() for installed guidance |
Function and method docstrings provide parameter details and runnable examples. Use help(pm.connect), help(pm.Source), or your editor's API lookup.