Skip to content

feat: Add support for agent invoke with no input, or Message input #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Unshure
Copy link
Member

@Unshure Unshure commented Aug 11, 2025

Description

Allow an agent to be invoked with no input, or a list of messages

This updates the agents execution to support additional invocation scenarios:

First, you can invoke an agent with just the current state of the messages array:

agent = Agent(messages=[{"role":"user", "content":[{"text":"Hello!"}]}])
agent()

Additionally, you can invoke the agent with an input list of messages:

agent = Agent()
agent([
  {"role":"user", "content":[{"text":"Hello!"}]},
  {"role":"assistant", "content":[{"text":"How are"}]},
])

This helps enable things like "putting words in claudes mouth".

Related Issues

N/A

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce warnings in consuming repositories: agents-docs, agents-tools, agents-cli

  • I ran hatch run prepare

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@mkmeral
Copy link
Contributor

mkmeral commented Aug 12, 2025

with this PR, what's the desired state when calling with None?

@@ -597,6 +624,34 @@ async def _execute_event_loop_cycle(self, invocation_state: dict[str, Any]) -> A
async for event in events:
yield event

def _convert_prompt_to_messages(self, prompt: str | list[ContentBlock] | Messages | None) -> Messages:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts on turning str | list[ContentBlock] | Messages | None into a type alias? Seems like we're copying it around and it might be helpful to have some Prompt (don't over index on the name) type

@@ -408,7 +408,7 @@ def end_event_loop_cycle_span(

def start_agent_span(
self,
message: Message,
messages: Messages,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlikely to be breaking anyone because of

def get_tracer() -> Tracer:
    """Get or create the global tracer.

    Returns:
        The global tracer instance.
    """
    global _tracer_instance

    if not _tracer_instance:
        _tracer_instance = Tracer()

    return _tracer_instance

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants