Skip to content

chunk_to_text_artifact

autogen.beta.a2a.mappers.events.chunk_to_text_artifact #

chunk_to_text_artifact(event, *, artifact_id, task_id, context_id, append=True, last_chunk=False, name=None, description=None, artifact_metadata=None, update_metadata=None)

Map a streaming text chunk to an A2A text-artifact event.

The artifact is structured to play well with A2A append-streaming: every chunk reuses the same artifact_id with append=True so the client side can concatenate them. Caller flips last_chunk on the final piece.

name / description / artifact_metadata populate the optional Artifact fields; update_metadata rides on the enclosing TaskArtifactUpdateEvent.

Source code in autogen/beta/a2a/mappers/events.py
def chunk_to_text_artifact(
    event: ModelMessageChunk,
    *,
    artifact_id: str,
    task_id: str,
    context_id: str,
    append: bool = True,
    last_chunk: bool = False,
    name: str | None = None,
    description: str | None = None,
    artifact_metadata: Mapping[str, Any] | None = None,
    update_metadata: Mapping[str, Any] | None = None,
) -> A2ATextArtifact:
    """Map a streaming text chunk to an A2A text-artifact event.

    The artifact is structured to play well with A2A append-streaming:
    every chunk reuses the same ``artifact_id`` with ``append=True`` so
    the client side can concatenate them. Caller flips ``last_chunk``
    on the final piece.

    ``name`` / ``description`` / ``artifact_metadata`` populate the
    optional ``Artifact`` fields; ``update_metadata`` rides on the
    enclosing ``TaskArtifactUpdateEvent``.
    """
    artifact = _build_artifact(
        artifact_id=artifact_id,
        parts=[Part(text=event.content)],
        name=name,
        description=description,
        artifact_metadata=artifact_metadata,
    )
    update = _build_artifact_update(
        task_id=task_id,
        context_id=context_id,
        artifact=artifact,
        append=append,
        last_chunk=last_chunk,
        update_metadata=update_metadata,
    )
    return A2ATextArtifact(
        update=update,
        append=append,
        last_chunk=last_chunk,
        text=event.content,
    )