Skip to main content

Python backend utilities for sharing app state with AI agents. New: {messages, actions, context} model for RAG-ready architecture.

Project description

agent-state-bridge (Python)

Python backend utilities for sharing app state with AI agents. Works with FastAPI, Flask, Django REST Framework, and any Python web framework.

Installation

# Basic installation (FastAPI is recommended)
pip install agent-state-bridge[fastapi]

# Or with Flask
pip install agent-state-bridge[flask]

# Or with Django
pip install agent-state-bridge[django]

# Or install all
pip install agent-state-bridge[all]

Quick Start

FastAPI

from fastapi import FastAPI
from agent_state_bridge.fastapi import create_agent_router

async def my_agent(message: str, state: dict) -> str:
    """Your agent logic here"""
    cart_items = state.get("cart", {}).get("items", [])
    return f"You have {len(cart_items)} items. You said: {message}"

app = FastAPI()
router = create_agent_router(my_agent, tags=["agent"])
app.include_router(router)

Flask

from flask import Flask
from agent_state_bridge.flask import create_agent_blueprint

def my_agent(message: str, state: dict) -> str:
    return f"Processed: {message}"

app = Flask(__name__)
bp = create_agent_blueprint(my_agent)
app.register_blueprint(bp)

Django REST Framework

from agent_state_bridge.django import agent_api_view

@agent_api_view
def my_agent(message: str, state: dict) -> str:
    return f"Processed: {message}"

# In urls.py:
# path('chat/', my_agent)

Integration with AI Frameworks

LangChain

from langchain_openai import ChatOpenAI
from agent_state_bridge.fastapi import create_agent_router

llm = ChatOpenAI(model="gpt-4o-mini")

async def langchain_agent(message: str, state: dict) -> str:
    response = await llm.ainvoke([{"role": "user", "content": message}])
    return response.content

router = create_agent_router(langchain_agent)

Microsoft Agent Framework

from azure.ai.projects import AIProjectClient
from agent_state_bridge.fastapi import create_agent_router

client = AIProjectClient(...)
agent = client.agents.create_agent(model="gpt-4o-mini")

async def agent_handler(message: str, state: dict) -> str:
    # Your Agent Framework logic
    return response

router = create_agent_router(agent_handler)

See /examples folder for complete examples with LangChain, Agent Framework, and more.

API Reference

Models

  • AgentRequest: Request model with message and state fields
  • AgentResponse: Response model with response field

FastAPI

  • create_agent_router(handler, prefix="", tags=[]): Create router with /chat endpoint
  • AgentBridge: Class-based approach with decorator

Flask

  • create_agent_blueprint(handler, name="agent", url_prefix=""): Create blueprint
  • @agent_route: Decorator for route handlers

Django

  • @agent_api_view: Decorator for function-based views
  • AgentAPIView: Base class for class-based views

Frontend Integration

This package works seamlessly with the npm package agent-state-bridge for React:

npm install agent-state-bridge

License

MIT

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

agent_state_bridge-0.2.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

agent_state_bridge-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file agent_state_bridge-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for agent_state_bridge-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7d23330c977c8e083fe683bdcb1ee42da355bd159c4596f884799855d0a04c7e
MD5 65eeb9514de584f420147c948fd0d4f4
BLAKE2b-256 1dcf18180e2405b0e7fe5b0e8d76d9570c0eff615579d6ced8b6036e9a6f3351

See more details on using hashes here.

File details

Details for the file agent_state_bridge-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for agent_state_bridge-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8fa2b1e35a62d08130c59b5cbb058db605bafe830ad927cbd8e75a3c0deca050
MD5 91286c8c6378dd6d477084d8e3e9a67a
BLAKE2b-256 097b1583faf933bef59d607ee68bdb1bba93f952854dd6601e06173706a34551

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