-
Notifications
You must be signed in to change notification settings - Fork 421
feat: replace kwargs with invocation_state in agent APIs #966
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
base: main
Are you sure you want to change the base?
Changes from all commits
0a8b1d4
f6ed2f2
967e805
63991c6
3929266
ebdeb5f
b01eeda
b50bca5
9900ccf
c2108f6
d584ad2
d6a1b5b
41338ec
45ef6ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import os | ||
import textwrap | ||
import unittest.mock | ||
import warnings | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
|
@@ -1877,3 +1878,58 @@ def test_tool(action: str) -> str: | |
assert '"action": "test_value"' in tool_call_text | ||
assert '"agent"' not in tool_call_text | ||
assert '"extra_param"' not in tool_call_text | ||
|
||
|
||
def test_agent__call__handles_none_invocation_state(mock_model, agent): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add one more unit test that can see what invocation state is passed to the event loop? I want an invocation like this:
To result in an invocation state like this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And similarly, agent("hello!", invocation_state={"my": "state"}) called with:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ^ Can you add the test case Mackenzie called out as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, see this test |
||
"""Test that agent handles None invocation_state without AttributeError.""" | ||
mock_model.mock_stream.return_value = [ | ||
{"contentBlockDelta": {"delta": {"text": "test response"}}}, | ||
{"contentBlockStop": {}}, | ||
] | ||
|
||
# This should not raise AttributeError: 'NoneType' object has no attribute 'get' | ||
result = agent("test", invocation_state=None) | ||
|
||
assert result.message["content"][0]["text"] == "test response" | ||
assert result.stop_reason == "end_turn" | ||
|
||
|
||
def test_agent__call__invocation_state_with_kwargs_deprecation_warning(agent, mock_event_loop_cycle): | ||
"""Test that kwargs trigger deprecation warning and are merged correctly with invocation_state.""" | ||
|
||
async def check_invocation_state(**kwargs): | ||
invocation_state = kwargs["invocation_state"] | ||
# Should have nested structure when both invocation_state and kwargs are provided | ||
assert invocation_state["invocation_state"] == {"my": "state"} | ||
assert invocation_state["other_kwarg"] == "foobar" | ||
yield EventLoopStopEvent("stop", {"role": "assistant", "content": [{"text": "Response"}]}, {}, {}) | ||
|
||
mock_event_loop_cycle.side_effect = check_invocation_state | ||
|
||
with warnings.catch_warnings(record=True) as captured_warnings: | ||
warnings.simplefilter("always") | ||
agent("hello!", invocation_state={"my": "state"}, other_kwarg="foobar") | ||
|
||
# Verify deprecation warning was issued | ||
assert len(captured_warnings) == 1 | ||
assert issubclass(captured_warnings[0].category, UserWarning) | ||
assert "`**kwargs` parameter is deprecating, use `invocation_state` instead." in str(captured_warnings[0].message) | ||
|
||
|
||
def test_agent__call__invocation_state_only_no_warning(agent, mock_event_loop_cycle): | ||
"""Test that using only invocation_state does not trigger warning and passes state directly.""" | ||
|
||
async def check_invocation_state(**kwargs): | ||
invocation_state = kwargs["invocation_state"] | ||
|
||
assert invocation_state["my"] == "state" | ||
assert "agent" in invocation_state | ||
yield EventLoopStopEvent("stop", {"role": "assistant", "content": [{"text": "Response"}]}, {}, {}) | ||
|
||
mock_event_loop_cycle.side_effect = check_invocation_state | ||
|
||
with warnings.catch_warnings(record=True) as captured_warnings: | ||
warnings.simplefilter("always") | ||
agent("hello!", invocation_state={"my": "state"}) | ||
|
||
assert len(captured_warnings) == 0 |
Uh oh!
There was an error while loading. Please reload this page.