Skip to content

ContextVariables

autogen.agentchat.group.context_variables.ContextVariables #

Bases: BaseModel

Stores and manages context variables for agentic workflows.

Utilises a dictionary-like interface for setting, getting, and removing variables.

data class-attribute instance-attribute #

data = Field(default_factory=dict)

get #

get(key, default=None)

Get a value from the context by key.

PARAMETER DESCRIPTION
key

The key to retrieve

TYPE: str

default

The default value to return if key is not found

TYPE: Optional[Any] DEFAULT: None

RETURNS DESCRIPTION
Optional[Any]

The value associated with the key or default if not found

Source code in autogen/agentchat/group/context_variables.py
def get(self, key: str, default: Optional[Any] = None) -> Optional[Any]:
    """
    Get a value from the context by key.

    Args:
        key: The key to retrieve
        default: The default value to return if key is not found

    Returns:
        The value associated with the key or default if not found
    """
    return self.data.get(key, default)

set #

set(key, value)

Set a value in the context by key.

PARAMETER DESCRIPTION
key

The key to set

TYPE: str

value

The value to store

TYPE: Any

Source code in autogen/agentchat/group/context_variables.py
def set(self, key: str, value: Any) -> None:
    """
    Set a value in the context by key.

    Args:
        key: The key to set
        value: The value to store
    """
    self.data[key] = value

remove #

remove(key)

Remove a key from the context.

PARAMETER DESCRIPTION
key

The key to remove

TYPE: str

RETURNS DESCRIPTION
bool

True if the key was removed, False if it didn't exist

Source code in autogen/agentchat/group/context_variables.py
def remove(self, key: str) -> bool:
    """
    Remove a key from the context.

    Args:
        key: The key to remove

    Returns:
        True if the key was removed, False if it didn't exist
    """
    if key in self.data:
        del self.data[key]
        return True
    return False

keys #

keys()

Get all keys in the context.

RETURNS DESCRIPTION
Iterable[str]

An iterable of all keys

Source code in autogen/agentchat/group/context_variables.py
def keys(self) -> Iterable[str]:
    """
    Get all keys in the context.

    Returns:
        An iterable of all keys
    """
    return self.data.keys()

values #

values()

Get all values in the context.

RETURNS DESCRIPTION
Iterable[Any]

An iterable of all values

Source code in autogen/agentchat/group/context_variables.py
def values(self) -> Iterable[Any]:
    """
    Get all values in the context.

    Returns:
        An iterable of all values
    """
    return self.data.values()

items #

items()

Get all key-value pairs in the context.

RETURNS DESCRIPTION
Iterable[tuple[str, Any]]

An iterable of all key-value pairs

Source code in autogen/agentchat/group/context_variables.py
def items(self) -> Iterable[tuple[str, Any]]:
    """
    Get all key-value pairs in the context.

    Returns:
        An iterable of all key-value pairs
    """
    return self.data.items()

clear #

clear()

Clear all keys and values from the context.

Source code in autogen/agentchat/group/context_variables.py
def clear(self) -> None:
    """Clear all keys and values from the context."""
    self.data.clear()

contains #

contains(key)

Check if a key exists in the context.

PARAMETER DESCRIPTION
key

The key to check

TYPE: str

RETURNS DESCRIPTION
bool

True if the key exists, False otherwise

Source code in autogen/agentchat/group/context_variables.py
def contains(self, key: str) -> bool:
    """
    Check if a key exists in the context.

    Args:
        key: The key to check

    Returns:
        True if the key exists, False otherwise
    """
    return key in self.data

update #

update(other)

Update context with key-value pairs from another dictionary.

PARAMETER DESCRIPTION
other

Dictionary containing key-value pairs to add

TYPE: dict[str, Any]

Source code in autogen/agentchat/group/context_variables.py
def update(self, other: dict[str, Any]) -> None:
    """
    Update context with key-value pairs from another dictionary.

    Args:
        other: Dictionary containing key-value pairs to add
    """
    self.data.update(other)

to_dict #

to_dict()

Convert context variables to a dictionary.

RETURNS DESCRIPTION
dict[str, Any]

Dictionary representation of all context variables

Source code in autogen/agentchat/group/context_variables.py
def to_dict(self) -> dict[str, Any]:
    """
    Convert context variables to a dictionary.

    Returns:
        Dictionary representation of all context variables
    """
    return self.data.copy()

from_dict classmethod #

from_dict(data)

Create a new ContextVariables instance from a dictionary.

E.g.: my_context = {"user_id": "12345", "settings": {"theme": "dark"}} context = ContextVariables.from_dict(my_context)

PARAMETER DESCRIPTION
data

Dictionary of key-value pairs

TYPE: dict[str, Any]

RETURNS DESCRIPTION
ContextVariables

New ContextVariables instance

Source code in autogen/agentchat/group/context_variables.py
@classmethod
def from_dict(cls, data: dict[str, Any]) -> "ContextVariables":
    """
    Create a new ContextVariables instance from a dictionary.

    E.g.:
    my_context = {"user_id": "12345", "settings": {"theme": "dark"}}
    context = ContextVariables.from_dict(my_context)

    Args:
        data: Dictionary of key-value pairs

    Returns:
        New ContextVariables instance
    """
    return cls(data=data)