-
Notifications
You must be signed in to change notification settings - Fork 1.2k
When starting run with message history ending in ModelRequest
, make its content available in RunContext.prompt
#2891
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
Conversation
When invoking an agent with only message_history, the latest ModelRequest gets popped off the history but wasn't made available in RunContext.prompt. This fix extracts UserPromptPart content from the popped message and sets it to ctx.deps.prompt. Also moves the build_run_context call to after message processing to ensure the context includes the extracted prompt content. Fixes #2876 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Douwe Maan <[email protected]>
Docs Preview
|
user_prompt_parts = [part for part in last_message.parts if isinstance(part, _messages.UserPromptPart)] | ||
if user_prompt_parts: | ||
# Join all UserPromptPart content (in case there are multiple) | ||
user_prompt_content = ' '.join(str(part.content) for part in user_prompt_parts) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude lol no, the type of UserPromptPart.content
is compatible with deps.prompt
, so we don't need to stringify
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude finished @DouweM's task —— View job
Todo List
- Read the code to understand type compatibility
- Acknowledge DouweM's correction about type compatibility
You're absolutely right! Looking at the type definitions:
UserPromptPart.content
is typed asstr | Sequence[UserContent]
ctx.deps.prompt
is typed asstr | Sequence[_messages.UserContent] | None
Since UserContent
and _messages.UserContent
are the same type, UserPromptPart.content
is indeed directly assignable to ctx.deps.prompt
without any stringification needed. The current code can be simplified to:
if user_prompt_parts:
# For single part, use content directly; for multiple parts, join them
if len(user_prompt_parts) == 1:
ctx.deps.prompt = user_prompt_parts[0].content
else:
# Join multiple parts as strings
ctx.deps.prompt = ' '.join(str(part.content) for part in user_prompt_parts)
Or even simpler, since the type system already handles this compatibility, the existing approach of joining works fine - the str()
call is just defensive but not strictly necessary for type safety.
Thanks for the correction! 🎯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claude I have no idea why you're joining strings anyway. Just combine all the user prompt parts' content
. And actually push into this PR please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Claude finished @DouweM's task —— View job
Todo List
- Read current implementation to understand the string joining issue
- Fix the user prompt parts combining logic in
_agent_graph.py
- Expand the test in
test_agent.py
to include ImageUrl as requested - Run tests to verify changes work
- Commit and push changes to the PR branch
Changes Made
Fixed user prompt parts combining logic in _agent_graph.py
:
- Removed unnecessary string joining and conversion
- For single
UserPromptPart
, directly assignpart.content
toctx.deps.prompt
- For multiple parts, properly combine their content as
Sequence[UserContent]
instead of joined strings - This maintains type compatibility since both
UserPromptPart.content
andctx.deps.prompt
usestr | Sequence[UserContent]
Expanded test to include ImageUrl:
- Updated
test_run_with_history_ending_on_model_request_and_no_user_prompt()
to usecontent=['Hello', ImageUrl('https://example.com/image.jpg')]
- Updated assertion to expect the full sequence including the ImageUrl
- Updated snapshot to match the new content structure
All history-related tests are passing. Changes have been committed and pushed to the PR branch.
tests/test_agent.py
Outdated
|
||
@agent.instructions | ||
async def instructions(ctx: RunContext) -> str: | ||
assert ctx.prompt == 'Hello' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Expand the test to also include an ImageUrl
and make sure it makes it here
…ing conversion - Remove string joining logic in favor of direct content assignment - For single UserPromptPart, assign content directly to ctx.deps.prompt - For multiple parts, combine as Sequence[UserContent] maintaining type compatibility - Expand test to include ImageUrl in content and verify proper handling Co-authored-by: Douwe Maan <[email protected]>
When invoking an agent with only message_history, the latest ModelRequest gets popped off the history but wasn't made available in RunContext.prompt. This fix extracts UserPromptPart content from the popped message and sets it to ctx.deps.prompt.
Also moves the build_run_context call to after message processing to ensure the context includes the extracted prompt content.
Fixes #2876
🤖 Generated with Claude Code