Skip to main content

Python Agent Graph Orchestration Engine - Build multi-agent workflows as executable graphs

Project description

Mesh Logo

Mesh: Python Agent Graph Orchestration Engine

Build agent and multi-agent workflows as executable graphs

License: MIT Python 3.11+


Mesh is a lightweight Python library for orchestrating multi-agent workflows as executable graphs. Build complex agent systems with token-by-token streaming, state management, and seamless integration with Vel and OpenAI Agents SDK.

Documentation

๐Ÿ“š View Full Documentation โ†’

Comprehensive guides and API reference:

Features

  • Graph-Based Workflows: Build agent workflows as directed graphs with controlled cycles
  • Dual API: Programmatic (LangGraph-style) and declarative (React Flow JSON) interfaces
  • Graph Visualization: Generate Mermaid diagrams with color-coded node types
  • Token-by-Token Streaming: Real-time streaming with provider-agnostic events
  • Advanced Event Types: Reasoning (o1/o3), usage tracking, citations, multi-modal files, custom events
  • Event Translation: Use Vel's standardized events or provider-native events
  • Multiple Agent Frameworks: Auto-detection for Vel and OpenAI Agents SDK
  • Multi-Provider Support: OpenAI, Anthropic, Google via Vel translation
  • 7 Core Node Types: Start, End, Agent, LLM, Tool, Condition, Loop
  • State Persistence: Pluggable backends (SQLite, in-memory, custom)
  • Variable Resolution: Template variables for dynamic workflows
  • Production Ready: Error handling, retries, and structured logging

Installation

# Basic installation
pip install agentmesh-py

# With OpenAI Agents SDK support
pip install agentmesh-py[agents]

# With FastAPI server support
pip install agentmesh-py[server]

# Development installation
pip install agentmesh-py[dev]

# All features
pip install agentmesh-py[all]

# With Vel SDK support (install separately)
pip install agentmesh-py
pip install git+https://github.com/rscheiwe/vel.git

Configuration

Create a .env file in your project root:

cp .env.example .env

Add your OpenAI API key:

OPENAI_API_KEY=sk-your-key-here

Load it in your code:

from mesh.utils import load_env
load_env()  # Loads variables from .env

Quick Start

Programmatic API (LangGraph-style)

import asyncio
from mesh import StateGraph, Executor, ExecutionContext, MemoryBackend
from mesh.nodes import LLMNode

async def main():
    # Build graph
    graph = StateGraph()
    graph.add_node("llm", None, node_type="llm", model="gpt-4")
    graph.add_edge("START", "llm")
    graph.set_entry_point("llm")

    # Compile and execute
    compiled = graph.compile()
    executor = Executor(compiled, MemoryBackend())
    context = ExecutionContext(
        graph_id="my-graph",
        session_id="session-1",
        chat_history=[],
        variables={},
        state={}
    )

    # Stream results
    async for event in executor.execute("What is 2+2?", context):
        if event.type == "token":
            print(event.content, end="", flush=True)

asyncio.run(main())

React Flow JSON (Flowise-compatible)

from mesh import ReactFlowParser, NodeRegistry, Executor

# Parse Flowise JSON
registry = NodeRegistry()
parser = ReactFlowParser(registry)
graph = parser.parse(flow_json)

# Execute
executor = Executor(graph, backend)
async for event in executor.execute(input_data, context):
    print(event)

FastAPI Server

from fastapi import FastAPI
from mesh import Executor, ExecutionContext
from mesh.streaming import SSEAdapter

app = FastAPI()

@app.post("/execute/stream")
async def execute_stream(request: ExecuteRequest):
    executor = Executor(graph, backend)
    context = ExecutionContext(...)

    adapter = SSEAdapter()
    return adapter.to_streaming_response(
        executor.execute(request.input, context)
    )

Core Concepts

Node Types

  1. StartNode: Entry point to the graph
  2. EndNode: Exit point from the graph
  3. AgentNode: Wraps Vel or OpenAI agents with auto-detection
  4. LLMNode: Direct LLM calls with streaming
  5. ToolNode: Execute Python functions
  6. ConditionNode: Conditional branching
  7. LoopNode: Iterate over arrays

Variable Resolution

Mesh supports template variables in node configurations:

  • {{$question}} - User input
  • {{node_id}} - Reference node output
  • {{node_id.field}} - Access nested fields
  • {{$vars.key}} - Global variables
  • {{$chat_history}} - Formatted chat history
  • {{$iteration}} - Current iteration value (in loops)

State Management

# In-memory (for development)
from mesh.backends import MemoryBackend
backend = MemoryBackend()

# SQLite (for production)
from mesh.backends import SQLiteBackend
backend = SQLiteBackend("mesh_state.db")

Advanced Usage

Conditional Branching

from mesh.nodes import Condition

def check_success(output):
    return "success" in str(output).lower()

graph.add_node("condition", [
    Condition("success", check_success, "success_handler"),
    Condition("failure", lambda x: not check_success(x), "error_handler"),
], node_type="condition")

Loop Processing

graph.add_node("loop", None, node_type="loop",
               array_path="$.items", max_iterations=100)

Custom Tools

def my_tool(input: str, multiplier: int = 2) -> str:
    return input * multiplier

graph.add_node("tool", my_tool, node_type="tool",
               config={"bindings": {"multiplier": 3}})

Agent Integration

# Vel Agent (with streaming)
from vel import Agent as VelAgent
vel_agent = VelAgent(
    id="assistant",
    model={
        "provider": "openai",
        "name": "gpt-4",
        "temperature": 0.7,
    },
)
graph.add_node("agent", vel_agent, node_type="agent")

# Stream the output
async for event in executor.execute("Hello!", context):
    if event.type == "token":
        print(event.content, end="", flush=True)

# OpenAI Agents SDK (with Vel translation by default)
from agents import Agent
openai_agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant"
)
graph.add_node("agent", openai_agent, node_type="agent", config={"model": "gpt-4"})

# Or use native events
graph.add_node("agent", openai_agent, node_type="agent",
               use_native_events=True, config={"model": "gpt-4"})

Event Translation

By default, Mesh uses Vel's event translation for consistent event handling across providers:

# Default: Vel-translated events (consistent across providers)
graph.add_node("agent", agent, node_type="agent")

# All providers emit the same event types
async for event in executor.execute(input, context):
    if event.type == "token":  # Same for OpenAI, Anthropic, Google
        print(event.content, end="", flush=True)

# Optional: Use provider's native events
graph.add_node("agent", agent, node_type="agent", use_native_events=True)

See Event Translation Guide for details.

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚           User Application                  โ”‚
โ”‚       (FastAPI/Flask/Django)                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚
                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚            MESH LIBRARY                     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”‚
โ”‚  โ”‚  Parsers     โ”‚    โ”‚  Builders    โ”‚      โ”‚
โ”‚  โ”‚  - ReactFlow โ”‚    โ”‚  - StateGraphโ”‚      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚
โ”‚                    โ–ผ                         โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”‚
โ”‚         โ”‚  Graph Compiler  โ”‚                โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                โ”‚
โ”‚                  โ–ผ                           โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”‚
โ”‚         โ”‚ Execution Engine โ”‚                โ”‚
โ”‚         โ”‚  - Queue-based   โ”‚                โ”‚
โ”‚         โ”‚  - Streaming     โ”‚                โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                โ”‚
โ”‚                  โ–ผ                           โ”‚
โ”‚      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”              โ”‚
โ”‚      โ”‚   Node Implementationsโ”‚              โ”‚
โ”‚      โ”‚  Agentโ”‚LLMโ”‚Toolโ”‚...   โ”‚              โ”‚
โ”‚      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Examples

See the examples/ directory:

Guides:

Development

# Clone repository
git clone https://github.com/rscheiwe/mesh.git
cd mesh

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -e ".[all]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=mesh --cov-report=html

# Format code
black mesh/
ruff check mesh/

# Type checking
mypy mesh/

For AI Assistants: See CLAUDE.md for comprehensive development context and architecture details.

Roadmap

  • WebSocket streaming support
  • Distributed execution
  • Visual graph editor
  • LangChain/LlamaIndex integration
  • Human-in-the-loop approval nodes
  • Sub-graph composition

Contributing

Contributions welcome! Please read our contributing guidelines and submit PRs.

License

MIT License - see LICENSE file for details.

Credits

Inspired by:

  • Flowise - React Flow execution patterns
  • LangGraph - StateGraph API design
  • Vel - Event translation layer

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

agentmesh_py-0.1.11.tar.gz (133.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

agentmesh_py-0.1.11-py3-none-any.whl (157.4 kB view details)

Uploaded Python 3

File details

Details for the file agentmesh_py-0.1.11.tar.gz.

File metadata

  • Download URL: agentmesh_py-0.1.11.tar.gz
  • Upload date:
  • Size: 133.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for agentmesh_py-0.1.11.tar.gz
Algorithm Hash digest
SHA256 e136420f04e173a7b03a6d4381bc616a901f0f85768f97787226447ed52c5e46
MD5 da42012009dbab13d48bd53daf932c2d
BLAKE2b-256 f0464317e8cfd58812f0cec1bc585991f7499e161cfe21f9fc0538d2546beea7

See more details on using hashes here.

File details

Details for the file agentmesh_py-0.1.11-py3-none-any.whl.

File metadata

  • Download URL: agentmesh_py-0.1.11-py3-none-any.whl
  • Upload date:
  • Size: 157.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for agentmesh_py-0.1.11-py3-none-any.whl
Algorithm Hash digest
SHA256 5dcd62c53856b8e576586edc5d1dfb036aa7ef5e36424a27704c20fdae6ee932
MD5 a912531c603cdfa835b14a367b54a770
BLAKE2b-256 f2378d86f9db698def4e94e3fd95982d841121375ac7d5f120325cb2ac3b9e03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page