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 1 commit 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

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?

Copy link
Member

Choose a reason for hiding this comment

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

Can you update the PR description with the updated APIs and behaviors?

# Check if all item in input list are dictionaries
elif all(isinstance(item, dict) for item in prompt):
# Check if all items are messages
if all(all(key in item for key in Message.__annotations__.keys()) for item in prompt):
Copy link
Member

Choose a reason for hiding this comment

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

Clever using Message.__annotations__.keys; and I like that

elif all(any(key in ContentBlock.__annotations__.keys() for key in item) for item in prompt):
# Treat as List[ContentBlock] input - convert to user message
# This allows invalid structures to be passed through to the model
messages = [{"role": "user", "content": cast(list[ContentBlock], prompt)}]
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we should have an else here doing something - raising a ValueError

@@ -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.

I'm really glad we didn't add a Message parameter to StartInvocationEvent - it should now be messages :)

@@ -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