Skip to main content

Gotong Python SDK — connect Python agents to an Gotong Hub over WebSocket.

Project description

gotong (Python SDK)

Connect Python agents to an Gotong Hub over WebSocket.

The Hub is the TypeScript reference implementation; this SDK speaks the same wire protocol (docs/PROTOCOL.md) so Python agents register into the same registry, get scheduled by the same scheduler, and appear in the same transcript as TypeScript or in-process agents.

Install

# from-source (recommended at this stage — PyPI publish is descoped)
pip install -e python-sdk/

# from PyPI — NOT yet published, decision tracked in
# https://github.com/Emir-Aksoy/Gotong/blob/main/.github/RELEASE-CHECKLIST.md
# pip install gotong

Requires Python ≥ 3.10.

Usage

import asyncio
from gotong import AgentParticipant, connect


class WriterAgent(AgentParticipant):
    def __init__(self) -> None:
        super().__init__(id="writer-py", capabilities=["draft"])

    async def handle_task(self, task: dict) -> dict:
        topic = task["payload"].get("topic", "?")
        return {"text": f"on {topic}: a sentence courtesy of Python."}


async def main() -> None:
    session = await connect(
        url="ws://127.0.0.1:4000",
        agents=[WriterAgent()],
        api_key="my-key",            # optional
        on_state_change=lambda s, info: print(f"[state] -> {s} {info or ''}"),
    )
    await session.wait_closed()


asyncio.run(main())

Subclass AgentParticipant, give it an id + capabilities, override handle_task. Both async def handle_task and plain def handle_task work — the SDK awaits whichever you wrote.

Adapters — bring your own agent framework

gotong.adapters wraps an external framework's object as an AgentParticipant, so the Hub routes Tasks to it like any other agent. The framework is a peer dependency — importing the adapter never pulls in langgraph / crewai; you install those yourself only for real graphs.

from langgraph.graph import StateGraph
from gotong import connect
from gotong.adapters import langgraph_participant

graph = build_graph().compile()          # any compiled StateGraph

agent = langgraph_participant(
    graph,
    id="researcher-lg",
    capabilities=["research"],
    # map the Gotong task <-> the graph's state dict (defaults pass the
    # payload straight through and return the whole final state)
    to_state=lambda task: {"question": task["payload"]["question"]},
    from_state=lambda state: {"answer": state["answer"]},
)

await connect(url="ws://127.0.0.1:4000", agents=[agent])

The graph is duck-typed (anything with .invoke(state)), .ainvoke is preferred when present, and a sync graph runs off the event loop so it can't stall the other agents on the connection.

A CrewAI crew wraps the same way — duck-typed on .kickoff(inputs), .kickoff_async preferred:

from crewai import Crew
from gotong.adapters import crewai_participant

crew = Crew(agents=[...], tasks=[...])
agent = crewai_participant(
    crew,
    id="market-research-crew",
    capabilities=["market-research"],
    to_inputs=lambda task: {"topic": task["payload"]["topic"]},
    from_output=lambda out: {"report": out.raw},   # default: {"text": out.raw}
)

What the SDK does

  • Opens a WebSocket to the Hub
  • Sends HELLO with your declared agents and optional apiKey
  • Awaits WELCOME, raises ConnectionRejected on REJECT
  • Dispatches TASK frames to the right agent's handle_task, sends back RESULT
  • Replies to PING with PONG, forwards CANCEL to on_task_cancelled
  • Auto-reconnects on transport failure with exponential backoff
  • Cleans up on close() (sends GOODBYE, closes the socket)

What it doesn't do (yet)

  • Publish/subscribe on channels (the Python agent is task-side only in v0.5)
  • Streaming results
  • Resume of in-flight tasks across reconnect (the Hub fails them as remote_disconnect, same as the Node SDK)

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

gotong-1.1.0.tar.gz (34.3 kB view details)

Uploaded Source

Built Distribution

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

gotong-1.1.0-py3-none-any.whl (24.6 kB view details)

Uploaded Python 3

File details

Details for the file gotong-1.1.0.tar.gz.

File metadata

  • Download URL: gotong-1.1.0.tar.gz
  • Upload date:
  • Size: 34.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for gotong-1.1.0.tar.gz
Algorithm Hash digest
SHA256 a86456f07d3df97a6385b48361c1c43e73a8adc9f56c51baf4421015680353b1
MD5 38c074f67383a8671f6274fbca3deb67
BLAKE2b-256 07bff46bf9060b98fca3d80ab3c3418a722344fa6c353b0a9d5741bc22b9fe2a

See more details on using hashes here.

File details

Details for the file gotong-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: gotong-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 24.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for gotong-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 81a4cd1e20835979c7d63937e993a4fc64b73fe9b50fe9b4247c5b9588c2e48c
MD5 64a32d2d1f11978b44bda57bd5208dfc
BLAKE2b-256 42fa155734fdd0a3675ba7102b8909363c85fbe944afe721a30bed781d46972a

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