Skip to main content

astropods-adapter-core

Framework-agnostic bridge between Python agents and the Astropods messaging service.

Installation

pip install astropods-adapter-core

Requires Python 3.10+.

Usage

If you're using a supported framework, use the pre-built adapter package instead (e.g. astropods-adapter-langchain). Use this package directly to connect a custom or unsupported framework.

Create a class with name, stream, and get_config, then call serve():

from astropods_adapter_core import StreamHooks, StreamOptions, serve

class MyAdapter:
    name = "My Agent"

    async def stream(self, prompt: str, hooks: StreamHooks, options: StreamOptions) -> None:
        try:
            hooks.on_chunk("Hello!")
            hooks.on_finish()
        except Exception as e:
            hooks.on_error(e)

    def get_config(self) -> dict:
        return {"system_prompt": "You are a helpful assistant.", "tools": []}

serve(MyAdapter())

serve() blocks until SIGINT or SIGTERM. Under ast dev, GRPC_SERVER_ADDR is injected automatically.

API

AgentAdapter protocol

Member Description
name: str Display name used in logs and registration
async stream(prompt, hooks, options) Stream a response, invoking hooks as the agent progresses
get_config() -> dict Return {"system_prompt": str, "tools": [...]} for playground display

StreamHooks

Call these inside stream() as the agent produces output:

Method When to call
on_chunk(text) Each text token or fragment from the LLM
on_status_update({"status": "..."}) Agent state change — valid values: THINKING, SEARCHING, GENERATING, PROCESSING, ANALYZING, CUSTOM
on_finish() Response complete — call exactly once per request
on_error(exception) Error occurred — call instead of on_finish

on_trace_context(trace_context) is optional; call it with getattr when a turn has W3C trace context.

For CUSTOM status, include "custom_message" in the dict:

hooks.on_status_update({"status": "CUSTOM", "custom_message": "Fetching data..."})

create_traceparent(*, trace_id, span_id, trace_flags="01")

Formats a native trace/span ID pair into a W3C traceparent string to hand to on_trace_context. Returns "" for invalid or all-zero IDs, so it's safe to pass raw OpenTelemetry span context. trace_flags accepts an int or hex string and defaults to "01" (sampled).

from astropods_messaging import TraceContext
from astropods_adapter_core import create_traceparent

ctx = span.get_span_context()
traceparent = create_traceparent(
    trace_id=f"{ctx.trace_id:032x}",
    span_id=f"{ctx.span_id:016x}",
    trace_flags=int(ctx.trace_flags),
)
if traceparent:
    on_trace_context = getattr(hooks, "on_trace_context", None)
    if on_trace_context is not None:
        on_trace_context(TraceContext(traceparent=traceparent))

StreamOptions

Per-request context passed to stream():

Field Description
conversation_id Stable ID for the conversation thread
user_id ID of the user who sent the message
platform_context Optional[PlatformContext] — platform-specific fields (channel, thread, workspace, event kind). None for messages from non-platform sources (playground, direct gRPC).

Using platform_context

PlatformContext exposes the source-event fields adapters often need to branch on — the channel and thread to reply into, the workspace, the bot's own user ID, and an event_kind enum that distinguishes a DM from an @-mention from a thread reply without having to inspect message content.

from astropods_adapter_core import PlatformContext, StreamHooks, StreamOptions

async def stream(self, prompt, hooks: StreamHooks, options: StreamOptions) -> None:
    pc = options.platform_context
    if pc and pc.event_kind == PlatformContext.EVENT_KIND_APP_MENTION:
        hooks.on_chunk(f"You @-mentioned me in {pc.channel_name or pc.channel_id}.")
    else:
        hooks.on_chunk("Hello!")
    hooks.on_finish()

Always null-check first — platform_context is None for messages from the playground or direct gRPC clients. See PlatformContext for the full field list.

serve(adapter, options?)

Connects the adapter to the messaging service and blocks until shutdown.

from astropods_adapter_core import ServeOptions, serve

# Override the gRPC address (default: GRPC_SERVER_ADDR env var or localhost:9090)
serve(adapter, ServeOptions(server_address="astro-messaging:9090"))

MessagingBridge

serve() is a thin wrapper around MessagingBridge. Use it directly if you need lifecycle control:

import asyncio
from astropods_adapter_core import MessagingBridge

bridge = MessagingBridge(adapter)
asyncio.run(bridge.start())

Download files

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

Source Distribution

astropods_adapter_core-0.7.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

astropods_adapter_core-0.7.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file astropods_adapter_core-0.7.0.tar.gz.

File metadata

  • Download URL: astropods_adapter_core-0.7.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for astropods_adapter_core-0.7.0.tar.gz
Algorithm Hash digest
SHA256 0431e4a6768c0ea4f80ef681975cf2fd36a38f2ae0f8255f12f017df7b843959
MD5 4b1d27f435b974a1308db1e2972d53ab
BLAKE2b-256 7273b52d8a71a5bcae53377e35977019a8b6745e3443e6a8efb065820c51d9af

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropods_adapter_core-0.7.0.tar.gz:

Publisher: publish-pypi-core.yml on astropods/adapters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file astropods_adapter_core-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for astropods_adapter_core-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 64d5e892ae0777f7828a0f14fb04ffd3678794375a6437e1f47647e29a49d169
MD5 b328270b2c7e0c48d7ee9662e40a5bed
BLAKE2b-256 c5b98d2be20e794574c19e58d29115cacac71ee7beb07a463b1ecf3fb72dd17c

See more details on using hashes here.

Provenance

The following attestation bundles were made for astropods_adapter_core-0.7.0-py3-none-any.whl:

Publisher: publish-pypi-core.yml on astropods/adapters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.7.0 This release

2 files

0.5.0

2 files

0.4.1

2 files

0.4.0

2 files

0.3.1

2 files

0.3.0

2 files

0.2.0

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page