Skip to content

Python API ​

Import pymalloy as pm. The root namespace exposes constructors, native runtime owners, language tools, the widget, and the common error base.

python
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 namesContract
table, sql, data, source, viewSource and View construction
col, lit, given, count, case, Expr, SortScalar expressions
draft, read_model, Source, View, DraftDraft composition and editing
connect, Connection, Model, Query, Result, ModelSourceRuntime and result contracts
parse, formatLanguage tools
MalloyWidgetWidget inputs, state, and browser assets
PyMalloyErrorDomain 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 ​

ModuleAdditional public values
pymalloy.authoringInput, source/view/draft constructors
pymalloy.expressionscall, number, scalar constructors
pymalloy.analysisReports, schemas, locations, and to_dict
pymalloy.errorsSpecific exceptions and ExecutionContext
pymalloy.validationDocumentationPolicy, Validation, DataCheck
pymalloy.resultResult, Column
pymalloy.widgetMalloyWidget
pymalloy.browserAsynchronous connections, models, queries, results, and runtime assets
pymalloy.exportNotebook preparation, renderers, and bundles
pymalloy.agentagent_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.