Skip to content

GenericContent

autogen.llm_clients.models.content_blocks.GenericContent #

Bases: BaseContent

Handles content blocks we don't have specific types for yet.

This is the KEY to forward compatibility: - When a provider adds a new content type (e.g., "reflection", "video_analysis") - We don't have a specific class defined yet - GenericContent catches it and preserves ALL fields using Pydantic's native extra='allow' - Users can access fields via attribute access or helper methods - Later we can add a specific typed class without breaking anything

Example

Provider returns new "reflection" type#

reflection = GenericContent( type="reflection", reflection="Upon reviewing...", confidence=0.87, corrections=["fix1", "fix2"] )

Access fields immediately#

print(reflection.type) # "reflection" print(reflection.reflection) # "Upon reviewing..." (attribute access) print(reflection.confidence) # 0.87 (attribute access)

Extract all fields#

print(reflection.get_all_fields()) # All fields as dict print(reflection.get_extra_fields()) # Only unknown fields

type instance-attribute #

type

data property #

data

Backward compatibility: access extra fields as .data

Deprecated: Use get_extra_fields() or model_extra instead.

extra class-attribute instance-attribute #

extra = Field(default_factory=dict)

Config #

extra class-attribute instance-attribute #

extra = 'allow'

get #

get(key, default=None)

Dict-style get for any field (defined or extra).

Example

content.get("reflection", "N/A") content.get("confidence", 0.0)

Source code in autogen/llm_clients/models/content_blocks.py
def get(self, key: str, default: Any = None) -> Any:
    """Dict-style get for any field (defined or extra).

    Example:
        content.get("reflection", "N/A")
        content.get("confidence", 0.0)
    """
    # Try model_extra first (unknown fields)
    if self.model_extra and key in self.model_extra:
        return self.model_extra[key]
    # Fall back to getattr for defined fields
    return getattr(self, key, default)

get_all_fields #

get_all_fields()

Get all fields (defined + extra) as a single dict.

This is equivalent to model_dump() but more explicitly named.

Example

all_data = content.get_all_fields()

Source code in autogen/llm_clients/models/content_blocks.py
def get_all_fields(self) -> dict[str, Any]:
    """Get all fields (defined + extra) as a single dict.

    This is equivalent to model_dump() but more explicitly named.

    Example:
        all_data = content.get_all_fields()
    """
    return self.model_dump()

get_extra_fields #

get_extra_fields()

Get only the extra (unknown) fields.

Example

extras = content.get_extra_fields() for key, value in extras.items(): print(f"{key}: {value}")

Source code in autogen/llm_clients/models/content_blocks.py
def get_extra_fields(self) -> dict[str, Any]:
    """Get only the extra (unknown) fields.

    Example:
        extras = content.get_extra_fields()
        for key, value in extras.items():
            print(f"{key}: {value}")
    """
    return self.model_extra if self.model_extra else {}

has_field #

has_field(key)

Check if field exists (defined or extra).

Example

if content.has_field("reflection"): print(content.reflection)

Source code in autogen/llm_clients/models/content_blocks.py
def has_field(self, key: str) -> bool:
    """Check if field exists (defined or extra).

    Example:
        if content.has_field("reflection"):
            print(content.reflection)
    """
    return hasattr(self, key)

get_text #

get_text()

Extract text representation of content block.

Override in subclasses to provide specific text extraction logic. Default returns empty string for content blocks without text.

Source code in autogen/llm_clients/models/content_blocks.py
def get_text(self) -> str:
    """Extract text representation of content block.

    Override in subclasses to provide specific text extraction logic.
    Default returns empty string for content blocks without text.
    """
    return ""