Adapters Reference
Technical reference for the adapter system that bridges Dockrion and AI frameworks.
AgentAdapter Protocol
Section titled “AgentAdapter Protocol”The core structural protocol (PEP 544) that all adapters must satisfy:
class AgentAdapter(Protocol): def load(self, entrypoint: str) -> None: ... def invoke(self, payload: Dict[str, Any]) -> Dict[str, Any]: ... def get_metadata(self) -> Dict[str, Any]: ...Implementations need not inherit from AgentAdapter — structural typing checks method signatures.
Extended Protocols
Section titled “Extended Protocols”| Protocol | Adds | Description |
|---|---|---|
StreamingAgentAdapter | invoke_stream(payload) -> Iterator[Dict] | Sync streaming |
AsyncAgentAdapter | ainvoke(payload) -> Dict | Async invocation |
StatefulAgentAdapter | invoke(payload, config=None) -> Dict | Config-aware invocation |
LangGraphAdapter
Section titled “LangGraphAdapter”For agents using LangGraph compiled state graphs.
Load Behavior
Section titled “Load Behavior”- Validates the entrypoint format (
module.path:factory_name) - Imports the module and calls the factory function
- Checks the returned object has a callable
.invoke()method - Optionally validates against
Pregel/CompiledStateGraphtypes (if strict) - Detects streaming support (
.stream()method) and async support
Invoke Behavior
Section titled “Invoke Behavior”- Clears/sets thread-local
StreamContext - Calls
runner.invoke(payload)(orrunner.invoke(payload, config)if supported) - Serializes the result with
serialize_for_json() - Validates the output is a dict
Streaming Behavior (invoke_stream)
Section titled “Streaming Behavior (invoke_stream)”- If no
.stream()method, falls back to singleinvoke()→ oneresultevent - Otherwise, creates a
StreamContext(queue mode) and optionalLangGraphBackend - Runs
.stream()in a worker thread, pushes events to a queue - Main async loop polls the queue and yields events
- Uses
events_filter.get_langgraph_stream_modes()to select LangGraph stream modes (messages,updates,custom,values) - Drains user-emitted events from
StreamContext
Metadata
Section titled “Metadata”{ "framework": "langgraph", "adapter_type": "langgraph", "adapter_version": "0.1.0", "loaded": True, "entrypoint": "app.graph:create_graph", "supports_streaming": True, "supports_async": False, "supports_config": True, "is_langgraph_type": True}HandlerAdapter
Section titled “HandlerAdapter”For plain Python callables (sync or async).
Load Behavior
Section titled “Load Behavior”- Validates the handler format (
module.path:callable_name) - Imports the module and retrieves the callable
- Checks it’s actually callable
- Detects if it’s async (
asyncio.iscoroutinefunction) - Inspects the signature for a
contextparameter (forStreamContextinjection)
Invoke Behavior
Section titled “Invoke Behavior”- Sets the current
StreamContext(if provided) - For sync handlers: calls
handler(payload)orhandler(payload, context=context) - For async handlers: runs via
asyncio.run()orThreadPoolExecutorif a loop exists - Validates the output is a dict
- Serializes with
serialize_for_json()
Streaming Behavior (invoke_stream)
Section titled “Streaming Behavior (invoke_stream)”- Calls
invoke()with a queue-modeStreamContext - Yields
{"type": "result", "data": ...}for the invoke result - Drains queued events from the
StreamContext - Yields each queued event as
{"type": "custom", ...}
Metadata
Section titled “Metadata”{ "framework": "custom", "adapter_type": "handler", "adapter_version": "0.1.0", "loaded": True, "handler_path": "app.service:handle", "is_async": False, "accepts_context": True, "signature": "(payload: dict, context=None) -> dict"}Adapter Registry
Section titled “Adapter Registry”| Function | Description |
|---|---|
get_adapter(framework) | Instantiate the adapter for a framework. Raises ValidationError for unknown frameworks. |
get_handler_adapter() | Shortcut for HandlerAdapter() |
register_adapter(framework, adapter_class) | Register a custom adapter. Checks for load, invoke, get_metadata methods. |
list_supported_frameworks() | Returns sorted list of registered framework names |
is_framework_supported(framework) | Check if a framework has a registered adapter |
Default Registry
Section titled “Default Registry”_ADAPTER_REGISTRY = { "langgraph": LangGraphAdapter, "custom": HandlerAdapter,}langchain is a valid framework in the schema but has no registered adapter yet.
Writing a Custom Adapter
Section titled “Writing a Custom Adapter”from dockrion_adapters import register_adapter
class MyFrameworkAdapter: def __init__(self): self._runner = None
def load(self, entrypoint: str) -> None: module_path, callable_name = entrypoint.rsplit(":", 1) # ... import and prepare ...
def invoke(self, payload: dict) -> dict: return self._runner.run(payload)
def get_metadata(self) -> dict: return {"framework": "my_framework", "adapter_type": "custom"}
register_adapter("my_framework", MyFrameworkAdapter)After registration, framework: my_framework works in Dockfiles.
Output Serialization
Section titled “Output Serialization”| Function | Description |
|---|---|
deep_serialize(obj, max_depth=50) | Recursively convert any Python object to JSON-serializable form. Handles: primitives, bytes, collections, Pydantic models, dataclasses, datetime, UUID, Decimal, Enum, Path, callables |
serialize_for_json(data: dict) | deep_serialize() then ensure dict output (wraps in {"result": ...} if needed) |
Error Hierarchy
Section titled “Error Hierarchy”All adapter errors extend AdapterError which extends DockrionError:
| Error | Code | When |
|---|---|---|
AdapterError | ADAPTER_ERROR | Base |
AdapterLoadError | ADAPTER_LOAD_ERROR | load() fails |
ModuleNotFoundError | MODULE_NOT_FOUND | Import fails |
CallableNotFoundError | CALLABLE_NOT_FOUND | Symbol not in module |
InvalidAgentError | INVALID_AGENT | Wrong shape |
AdapterNotLoadedError | ADAPTER_NOT_LOADED | invoke() before load() |
AgentExecutionError | AGENT_EXECUTION_ERROR | Runtime error in agent |
AgentCrashedError | AGENT_CRASHED | Unexpected crash |
InvalidOutputError | INVALID_OUTPUT | Non-dict return |
Source:
packages/adapters/dockrion_adapters/
Previous: SDK Reference | Next: Error Hierarchy →