microsoft/autogen · error · ValueError
Runtime must be running when publishing message.
Error message
Runtime must be running when publishing message.
What it means
GrpcWorkerAgentRuntime.publish_message requires the runtime to be running: if _running is False it raises ValueError('Runtime must be running when publishing message.') since publishing needs the host connection established by start().
Source
Thrown at python/packages/autogen-ext/src/autogen_ext/runtimes/grpc/_worker_runtime.py:423
# TODO: Find a way to handle timeouts/errors
task = asyncio.create_task(self._send_message(runtime_message, "send", recipient, telemetry_metadata))
self._background_tasks.add(task)
task.add_done_callback(self._raise_on_exception)
task.add_done_callback(self._background_tasks.discard)
return await future
async def publish_message(
self,
message: Any,
topic_id: TopicId,
*,
sender: AgentId | None = None,
cancellation_token: CancellationToken | None = None,
message_id: str | None = None,
) -> None:
if not self._running:
raise ValueError("Runtime must be running when publishing message.")
if self._host_connection is None:
raise RuntimeError("Host connection is not set.")
if message_id is None:
message_id = str(uuid.uuid4())
message_type = self._serialization_registry.type_name(message)
with self._trace_helper.trace_block(
"create", topic_id, parent=None, extraAttributes={"message_type": message_type}
):
serialized_message = self._serialization_registry.serialize(
message, type_name=message_type, data_content_type=self._payload_serialization_format
)
sender_id = sender or AgentId("unknown", "unknown")
attributes = {
_constants.DATA_CONTENT_TYPE_ATTR: cloudevent_pb2.CloudEvent.CloudEventAttributeValue(
ce_string=self._payload_serialization_format
),View on GitHub (pinned to 027ecf0a37)
Solutions
- Await runtime.start() before any publish_message call
- Move eager publishes from constructors into the agent's first message handler or an explicit on_start hook
- Gate publishers on runtime state during shutdown to avoid racing stop()
Example fix
# before await runtime.publish_message(event, topic) # before start() await runtime.start() # after await runtime.start() await runtime.publish_message(event, topic)
Defensive patterns
Strategy: validation
Validate before calling
if not started:
await runtime.start()
started = True
await runtime.publish_message(event, topic) Try / catch
try:
await runtime.publish_message(event, topic)
except ValueError as e:
if "must be running" in str(e):
await runtime.start()
await runtime.publish_message(event, topic)
else:
raise Prevention
- Await start() before publishing
- Delay publishers (timers, queues) until after startup completes
- Stop background publishers before calling stop()
When it happens
Trigger: Calling `await runtime.publish_message(msg, topic_id)` before await runtime.start() completes or after stop(); registering a callback/type that publishes during registration before start.
Common situations: Publishing from module-level or __init__ code of an agent or service before the runtime started; shutdown ordering where background publishers outlive stop(); notebook cell re-runs.
Related errors
- Connection is not open.
- Runtime is already running.
- Runtime is not running.
- Host connection is not set.
- Runtime must be running when sending message.
AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15).
Data as JSON: /api/errors/f322e7b558914712.
Report an issue: GitHub.