/prometheus-python-skills:pyo3-bridgefrom prometheus-python-skills
PyO3 0.22 Rust-to-Python bridging patterns for Prometheus AGS. Covers exposing Rust functions and types to Python via #[pyfunction] and #[pyclass], the maturin build workflow, GIL management, async bridge patterns via pyo3-asyncio, and the primary use case: generating skill executor code in Python that calls into Rust crates (forge-rs, pk-librarian, surreal-memory) for use in Dify and custom skill servers.
Ships in prometheus-python-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
PyO3 bridges are used in two contexts:
Skill executor code generation — forge-rs generates Python scripts that call into Rust crates via PyO3 bindings. These scripts become executable tool code in Dify knowledge base workflows, Claude Code slash commands, and MCP skill servers.
Python MCP tool servers — skill executor servers in Python that expose Rust capabilities (inference, memory, policy evaluation) without requiring the caller to build Rust.
my-python-bridge/
├── Cargo.toml ← [lib] crate-type = ["cdylib"]
├── src/
│ └── lib.rs ← #[pymodule] + #[pyfunction] definitions
├── pyproject.toml ← maturin build config
├── python/
│ └── my_bridge/
│ └── __init__.py ← re-exports from the compiled .so
└── tests/
└── test_bridge.py
[package]
name = "my-python-bridge"
version = "0.1.0"
edition = "2021"
[lib]
name = "my_python_bridge"
crate-type = ["cdylib"]
[dependencies]
pyo3 = { version = "0.22", features = ["extension-module"] }
# Add forge-core, pk-core etc. as needed
forge-core = { path = "../tools/forge-rs/crates/forge-core" }
[features]
# Required for PyO3 to build correctly on all platforms
default = ["pyo3/extension-module"]
// src/lib.rs
use pyo3::prelude::*;
/// A function exposed to Python.
/// The docstring becomes the Python __doc__.
#[pyfunction]
fn enrich_task(task_path: &str, language: &str) -> PyResult<String> {
// Call into Rust logic — no async here (sync bridge)
let lang = language.parse::<forge_core::Language>()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyValueError, _>(e.to_string()))?;
// For now, return a placeholder — real impl would call forge-enricher
Ok(format!("Enriched {} for {}", task_path, lang.as_str()))
}
/// Exposing a Rust struct as a Python class.
#[pyclass]
struct SkillInfo {
#[pyo3(get)]
pub name: String,
#[pyo3(get)]
pub language: String,
}
#[pymethods]
impl SkillInfo {
#[new]
fn new(name: String, language: String) -> Self {
Self { name, language }
}
fn __repr__(&self) -> String {
format!("SkillInfo(name={:?}, language={:?})", self.name, self.language)
}
}
/// The Python module definition.
#[pymodule]
fn my_python_bridge(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(enrich_task, m)?)?;
m.add_class::<SkillInfo>()?;
Ok(())
}
PyO3 acquires the GIL when Rust calls into Python. For long-running Rust operations, release the GIL to allow other Python threads to run:
#[pyfunction]
fn heavy_computation(py: Python<'_>, data: Vec<f32>) -> PyResult<Vec<f32>> {
// Release GIL during the heavy work
py.allow_threads(|| {
// This block runs without the GIL
data.iter().map(|x| x * 2.0).collect::<Vec<_>>()
});
// GIL re-acquired here
Ok(result)
}
For exposing async Rust functions to Python's asyncio:
[dependencies]
pyo3-asyncio = { version = "0.21", features = ["tokio-runtime"] }
use pyo3_asyncio::tokio::future_into_py;
#[pyfunction]
fn async_enrich<'py>(py: Python<'py>, task_path: String) -> PyResult<Bound<'py, PyAny>> {
future_into_py(py, async move {
let result = forge_enricher::enrich_task_simple(&task_path)
.await
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
Ok(result)
})
}
# pyproject.toml
[build-system]
requires = ["maturin>=1.7,<2.0"]
build-backend = "maturin"
[project]
name = "my-python-bridge"
version = "0.1.0"
requires-python = ">=3.12"
[tool.maturin]
features = ["pyo3/extension-module"]
# Development (editable install)
uv pip install maturin
maturin develop
# Production wheel
maturin build --release
# Install the wheel
uv pip install target/wheels/my_python_bridge-*.whl
When forge-rs generates Python executor code for a skill, it produces this pattern:
# Generated by forge-rs — do not edit manually
# forge:skill=rust/performance, forge:task=CHANGE-042
import my_python_bridge as forge
def execute_skill(task_path: str, language: str = "rust") -> dict:
"""Execute the forge enrichment for this task."""
context = forge.enrich_task(task_path, language)
return {
"context": context,
"skill": "rust/performance",
"status": "enriched",
}
if __name__ == "__main__":
import json, sys
result = execute_skill(sys.argv[1])
print(json.dumps(result))
This script is the "executor" registered with a skill server. It can be called from Dify workflows, MCP tool servers, or Claude Code slash commands without requiring the caller to understand Rust.
import * — explicit imports onlyexcept: without exception type — always except SomeError:Py<T>) across threads without proper GIL managementpyo3::Python::with_gil() inside a py.allow_threads() block — deadlockmaturin develop before testing — the .so must be recompiled after Rust changesWhat SKILL.md sets; muted values are Claude Code's defaults
2 plugins in one repository ship this exact skill · first indexed Nov 27, 2026
Other plugins' skills for the same job
npx claudepluginhub prometheus-ags/prometheus-skill-system --plugin prometheus-python-skills