Overview
Concrete tool for serializing AutoGen components to portable JSON-compatible models provided by the autogen-core package.
Description
The dump_component() method is defined on ComponentToConfig (and inherited by the Component base class). It serializes any AutoGen component instance into a ComponentModel containing the provider class path, component type, version, description, label, and configuration dictionary. The method calls the instance's _to_config() to produce the config Pydantic model, then dumps it to a dictionary excluding None values. It resolves the provider string from the class path (or uses component_provider_override if set), and includes metadata from class variables or docstrings.
Usage
Use dump_component() on any AutoGen component (agents, teams, models, tools, termination conditions) when you need to serialize it for storage in a database, export to a JSON file, transmission over a network, or registration in a component gallery.
Code Reference
Source Location
- Repository: Microsoft AutoGen
- File:
python/packages/autogen-core/src/autogen_core/_component_config.py (L117-L161)
Signature
class ComponentToConfig(Generic[ToConfigT]):
component_type: ClassVar[ComponentType]
component_version: ClassVar[int] = 1
component_provider_override: ClassVar[str | None] = None
component_description: ClassVar[str | None] = None
component_label: ClassVar[str | None] = None
def _to_config(self) -> ToConfigT: ...
def dump_component(self) -> ComponentModel:
"""Dump the component to a model that can be loaded back in.
Raises:
TypeError: If the component is a local class.
Returns:
ComponentModel: The model representing the component.
"""
...
Import
from autogen_core import Component, ComponentModel
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| (self) |
ComponentToConfig instance |
Yes |
The component instance to serialize; must implement _to_config() and have component_type defined
|
Outputs
| Name |
Type |
Description
|
| ComponentModel |
ComponentModel |
Serialized component containing provider (str), component_type (str), version (int), component_version (int), description (str or None), label (str), and config (dict)
|
ComponentModel Fields
| Field |
Type |
Description
|
| provider |
str |
Fully-qualified Python class path (e.g., "autogen_agentchat.agents.AssistantAgent")
|
| component_type |
ComponentType or None |
Logical type: "model", "agent", "tool", "termination", "token_provider", "workbench", or custom string
|
| version |
int or None |
Schema version of the component specification
|
| component_version |
int or None |
Version of the component implementation
|
| description |
str or None |
Human-readable description (defaults to class docstring if not set)
|
| label |
str or None |
Human-readable label (defaults to class name if not set)
|
| config |
dict[str, Any] |
Configuration dictionary from _to_config().model_dump(exclude_none=True)
|
Usage Examples
Basic Example
import json
from autogen_core import ComponentModel
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
# Serialize a model client
model = OpenAIChatCompletionClient(model="gpt-4o-mini")
model_component = model.dump_component()
print(f"Provider: {model_component.provider}")
print(f"Type: {model_component.component_type}")
print(f"Config keys: {list(model_component.config.keys())}")
# Serialize an agent
agent = AssistantAgent(
name="assistant",
model_client=model,
system_message="You are helpful.",
)
agent_component = agent.dump_component()
# Serialize a full team
termination = MaxMessageTermination(max_messages=10)
team = RoundRobinGroupChat(participants=[agent], termination_condition=termination)
team_component = team.dump_component()
# Export to JSON
with open("team.json", "w") as f:
f.write(team_component.model_dump_json(indent=2))
# Round-trip: load it back
from autogen_agentchat.teams import BaseGroupChat
loaded_team = BaseGroupChat.load_component(team_component)
print(f"Loaded team type: {type(loaded_team).__name__}")
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.