***

title: to_dict
slug: /reference/python/agents/context-builder/to-dict
description: Convert all contexts to a dictionary for SWML generation.
max-toc-depth: 3
---------------------

For a complete index of all SignalWire documentation pages, fetch https://signalwire.com/docs/llms.txt

Convert all contexts to a dictionary for SWML generation. Calls `validate()`
first and raises `ValueError` if the configuration is invalid.

## **Returns**

`dict[str, Any]` -- A mapping of context names to their serialized dictionaries,
ready for inclusion in the SWML document.

## **Example**

```python {9}
from signalwire import AgentBase

agent = AgentBase(name="my-agent", route="/agent")

contexts = agent.define_contexts()
ctx = contexts.add_context("default")
ctx.add_step("greet").set_text("Hello!")

data = contexts.to_dict()
# {"default": {"steps": [{"name": "greet", "text": "Hello!"}]}}

agent.serve()
```