generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 460
fix: Strip argument sections out of inputSpec top-level description #1142
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
zastrowm
wants to merge
2
commits into
strands-agents:main
Choose a base branch
from
zastrowm:strip_args_from_input_spec_description
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -221,14 +221,7 @@ def test_tool(param1: str, param2: int) -> str: | |
|
|
||
| # Check basic spec properties | ||
| assert spec["name"] == "test_tool" | ||
| assert ( | ||
| spec["description"] | ||
| == """Test tool function. | ||
|
|
||
| Args: | ||
| param1: First parameter | ||
| param2: Second parameter""" | ||
| ) | ||
| assert spec["description"] == "Test tool function." | ||
|
|
||
| # Check input schema | ||
| schema = spec["inputSchema"]["json"] | ||
|
|
@@ -310,6 +303,174 @@ def test_tool(required: str, optional: Optional[int] = None) -> str: | |
| exp_events = [ | ||
| ToolResultEvent({"toolUseId": "test-id", "status": "success", "content": [{"text": "Result: hello 42"}]}) | ||
| ] | ||
| assert tru_events == exp_events | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_docstring_description_extraction(): | ||
| """Test that docstring descriptions are extracted correctly, excluding Args section.""" | ||
|
|
||
| @strands.tool | ||
| def tool_with_full_docstring(param1: str, param2: int) -> str: | ||
| """This is the main description. | ||
|
|
||
| This is more description text. | ||
|
|
||
| Args: | ||
| param1: First parameter | ||
| param2: Second parameter | ||
|
|
||
| Returns: | ||
| A string result | ||
|
|
||
| Raises: | ||
| ValueError: If something goes wrong | ||
| """ | ||
| return f"{param1} {param2}" | ||
|
|
||
| spec = tool_with_full_docstring.tool_spec | ||
| assert ( | ||
| spec["description"] | ||
| == """This is the main description. | ||
|
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. Nit: So that you can present this more cleanly in code, you can use textwrap.dedent: import textwrap
...
description = textwrap.dedent("""
This is the main description.
...
""") |
||
|
|
||
| This is more description text. | ||
|
|
||
| Returns: | ||
| A string result | ||
|
|
||
| Raises: | ||
| ValueError: If something goes wrong""" | ||
| ) | ||
|
|
||
|
|
||
| def test_docstring_args_variations(): | ||
| """Test that various Args section formats are properly excluded.""" | ||
|
|
||
| @strands.tool | ||
| def tool_with_args(param: str) -> str: | ||
| """Main description. | ||
|
|
||
| Args: | ||
| param: Parameter description | ||
| """ | ||
| return param | ||
|
|
||
| @strands.tool | ||
| def tool_with_arguments(param: str) -> str: | ||
| """Main description. | ||
|
|
||
| Arguments: | ||
| param: Parameter description | ||
| """ | ||
| return param | ||
|
|
||
| @strands.tool | ||
| def tool_with_parameters(param: str) -> str: | ||
| """Main description. | ||
|
|
||
| Parameters: | ||
| param: Parameter description | ||
| """ | ||
| return param | ||
|
|
||
| @strands.tool | ||
| def tool_with_params(param: str) -> str: | ||
| """Main description. | ||
|
|
||
| Params: | ||
| param: Parameter description | ||
| """ | ||
| return param | ||
|
|
||
| for tool in [tool_with_args, tool_with_arguments, tool_with_parameters, tool_with_params]: | ||
| spec = tool.tool_spec | ||
| assert spec["description"] == "Main description." | ||
|
|
||
|
|
||
| def test_docstring_no_args_section(): | ||
| """Test docstring extraction when there's no Args section.""" | ||
|
|
||
| @strands.tool | ||
| def tool_no_args(param: str) -> str: | ||
| """This is the complete description. | ||
|
|
||
| Returns: | ||
| A string result | ||
| """ | ||
| return param | ||
|
|
||
| spec = tool_no_args.tool_spec | ||
| expected_desc = """This is the complete description. | ||
|
|
||
| Returns: | ||
| A string result""" | ||
| assert spec["description"] == expected_desc | ||
|
|
||
|
|
||
| def test_docstring_only_args_section(): | ||
| """Test docstring extraction when there's only an Args section.""" | ||
|
|
||
| @strands.tool | ||
| def tool_only_args(param: str) -> str: | ||
| """Args: | ||
| param: Parameter description | ||
| """ | ||
| return param | ||
|
|
||
| spec = tool_only_args.tool_spec | ||
| # Should fall back to function name when no description remains | ||
| assert spec["description"] == "tool_only_args" | ||
|
|
||
|
|
||
| def test_docstring_empty(): | ||
| """Test docstring extraction when docstring is empty.""" | ||
|
|
||
| @strands.tool | ||
| def tool_empty_docstring(param: str) -> str: | ||
| return param | ||
|
|
||
| spec = tool_empty_docstring.tool_spec | ||
| # Should fall back to function name | ||
| assert spec["description"] == "tool_empty_docstring" | ||
|
|
||
|
|
||
| def test_docstring_preserves_other_sections(): | ||
| """Test that non-Args sections are preserved in the description.""" | ||
|
|
||
| @strands.tool | ||
| def tool_multiple_sections(param: str) -> str: | ||
| """Main description here. | ||
|
|
||
| Args: | ||
| param: This should be excluded | ||
|
|
||
| Returns: | ||
| This should be included | ||
|
|
||
| Raises: | ||
| ValueError: This should be included | ||
|
|
||
| Examples: | ||
| This should be included | ||
|
|
||
| Note: | ||
| This should be included | ||
| """ | ||
| return param | ||
|
|
||
| spec = tool_multiple_sections.tool_spec | ||
| description = spec["description"] | ||
|
|
||
| # Should include main description and other sections | ||
| assert "Main description here." in description | ||
| assert "Returns:" in description | ||
| assert "This should be included" in description | ||
| assert "Raises:" in description | ||
| assert "Examples:" in description | ||
| assert "Note:" in description | ||
|
|
||
| # Should exclude Args section | ||
| assert "This should be excluded" not in description | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Consideration: Not saying it needs to be done for this PR, but I'm wondering if there is a reliable third party library out there that we can use to parse the docstrings. From a cursory search, I came across https://pypi.org/project/docstring-parser/. It's in preview though.
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.
Yeah - I also looked for built in ways to do this and didn't see any great ways. I think if we were starting today we'd do this differently so that we're not managing raw strings.
I'm a bit reluctant to add a library just this :(