Skip to content

AgentReply

autogen.beta.agent.AgentReply #

AgentReply(response, *, context, client, agent, provider, response_schema)

Bases: Generic[TResult, TAgent]

Source code in autogen/beta/agent.py
def __init__(
    self,
    response: ModelResponse,
    *,
    context: Context,
    client: LLMClient,
    agent: "Agent[TAgent]",
    provider: Provider | None,
    response_schema: ResponseProto[TResult] | None,
) -> None:
    self.response = response
    self.context = context
    self.__client = client
    self.__agent = agent
    self.__provider = provider
    self.__schema = response_schema

response instance-attribute #

response = response

context instance-attribute #

context = context

body property #

body

Text body of the model's response for this turn.

files property #

files

Images generated by the model in this turn (decoded bytes).

history property #

history

content async #

content(*, retries=0)
Source code in autogen/beta/agent.py
async def content(
    self,
    *,
    retries: int | float = 0,
) -> TResult | None:
    schema = self.__schema
    if schema is None:
        return self.body  # type: ignore[return-value]

    max_retries = max(retries, 0)

    current = self
    attempt = 0

    while True:
        if current.body is None:
            return None

        attempt += 1
        try:
            return await schema.validate(
                current.body,
                context=current.context,
                provider=current.__provider,
            )
        except ValidationError as e:
            if attempt > max_retries:
                raise e

            schema_section = (
                f"\n\n== Schema ==\n{json.dumps(schema.json_schema)}." if schema.json_schema is not None else ""
            )
            current = await current.ask(
                "Your previous response could not be validated by schema."
                f"{schema_section}"
                "\n\nPlease try again."
                "\n\n== Validation Error ==\n"
                f"{e.json()}",
                response_schema=schema,
            )

ask async #

ask(*msg: str | Input, dependencies: dict[Any, Any] | None = ..., variables: dict[Any, Any] | None = ..., prompt: Iterable[str] = ..., config: ModelConfig | None = ..., tools: Iterable[Tool] = ..., middleware: Iterable[MiddlewareFactory] = ..., observers: Iterable[Observer] = ..., response_schema: type[T2], hitl_hook: HumanHook | None = ...) -> AgentReply[T2, TAgent]
ask(*msg: str | Input, dependencies: dict[Any, Any] | None = ..., variables: dict[Any, Any] | None = ..., prompt: Iterable[str] = ..., config: ModelConfig | None = ..., tools: Iterable[Tool] = ..., middleware: Iterable[MiddlewareFactory] = ..., observers: Iterable[Observer] = ..., response_schema: ResponseProto[T2], hitl_hook: HumanHook | None = ...) -> AgentReply[T2, TAgent]
ask(*msg: str | Input, dependencies: dict[Any, Any] | None = ..., variables: dict[Any, Any] | None = ..., prompt: Iterable[str] = ..., config: ModelConfig | None = ..., tools: Iterable[Tool] = ..., middleware: Iterable[MiddlewareFactory] = ..., observers: Iterable[Observer] = ..., response_schema: None, hitl_hook: HumanHook | None = ...) -> AgentReply[str, TAgent]
ask(*msg: str | Input, dependencies: dict[Any, Any] | None = ..., variables: dict[Any, Any] | None = ..., prompt: Iterable[str] = ..., config: ModelConfig | None = ..., tools: Iterable[Tool] = ..., middleware: Iterable[MiddlewareFactory] = ..., observers: Iterable[Observer] = ..., hitl_hook: HumanHook | None = ...) -> AgentReply[TAgent, TAgent]
ask(*msg, dependencies=None, variables=None, prompt=(), config=None, tools=(), middleware=(), observers=(), response_schema=omit, hitl_hook=None)
Source code in autogen/beta/agent.py
async def ask(
    self,
    *msg: str | Input,
    dependencies: dict[Any, Any] | None = None,
    variables: dict[Any, Any] | None = None,
    prompt: Iterable[str] = (),
    config: ModelConfig | None = None,
    tools: Iterable[Tool] = (),
    middleware: Iterable[MiddlewareFactory] = (),
    observers: Iterable[Observer] = (),
    response_schema: Omittable[ResponseProto[Any] | type | None] = omit,
    hitl_hook: HumanHook | None = None,
) -> "AgentReply[Any, Any]":
    initial_event = ModelRequest.ensure_request(list(msg))

    context = self.context
    if dependencies:
        context.dependencies.update(dependencies)
    if variables:
        context.variables.update(variables)
    if prompt:
        context.prompt = list(prompt)

    client = config.create() if config else self.__client

    return await self.__agent._execute(
        initial_event,
        context=context,
        client=client,
        hitl_hook=hitl_hook,
        additional_tools=tools,
        additional_middleware=middleware,
        additional_observers=observers,
        response_schema=response_schema,
    )