Skip to content

Commit 61a7a7b

Browse files
Use full "Returns" section when generating the function description (#11772)
* mcp-parse-returns * func desc * add changeset * fix failing test --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent e16e45c commit 61a7a7b

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

.changeset/dark-pets-decide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gradio": minor
3+
---
4+
5+
feat:Use full "Returns" section when generating the function description

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ demo/unload_event_test/output_log.txt
5858
demo/stream_video_out/output_*.ts
5959
demo/stream_video_out/output_*.mp4
6060
demo/stream_audio_out/*.mp3
61+
test-demo/*
6162
#demo/image_editor_story/*.png
6263

6364
# Etc

gradio/utils.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,8 +1704,9 @@ def get_function_description(fn: Callable) -> tuple[str, dict[str, str], list[st
17041704
Get the description of a function, its parameters, and return values by parsing the docstring.
17051705
The docstring should be formatted as follows: first lines are the description
17061706
of the function, then a line starts with "Args:", "Parameters:", or "Arguments:",
1707-
followed by lines of the form "param_name: description", then optionally a line
1708-
that starts with "Returns:" followed by descriptions of return values.
1707+
followed by lines of the form "param_name: description", then optionally lines
1708+
that starts with "Returns:" followed by descriptions of return values. All lines
1709+
after the "Returns:" line are added in the `returns` list (including e.g. "Examples").
17091710
17101711
Parameters:
17111712
fn: The function to get the docstring for.
@@ -1782,16 +1783,7 @@ def get_function_description(fn: Callable) -> tuple[str, dict[str, str], list[st
17821783
if not line:
17831784
continue
17841785

1785-
try:
1786-
if line.startswith("-"):
1787-
returns.append(line[1:].strip())
1788-
elif ":" in line:
1789-
_, return_desc = line.split(":", 1)
1790-
returns.append(return_desc.strip())
1791-
else:
1792-
returns.append(line)
1793-
except Exception:
1794-
continue
1786+
returns.append(line)
17951787

17961788
except Exception:
17971789
pass

test/test_mcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def test_mcp_sse_transport(test_mcp_app):
185185
assert response.json() == [
186186
{
187187
"name": "test_tool",
188-
"description": "This is a test tool. Returns: the original value as a string",
188+
"description": "This is a test tool. Returns: - the original value as a string",
189189
"inputSchema": {
190190
"type": "object",
191191
"properties": {"x": {"type": "string", "description": ""}},

test/test_utils.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,33 @@ def test_func(param1, param2):
785785
description, parameters, returns = get_function_description(test_func)
786786
assert description == "This is a test function."
787787
assert parameters == {"param1": "First parameter", "param2": "Second parameter"}
788-
assert returns == ["First return value", "Second return value"]
788+
assert returns == ["- First return value", "- Second return value"]
789+
790+
def test_function_with_extended_returns(self):
791+
def test_func(param1, param2):
792+
"""This is a test function.
793+
Args:
794+
param1: First parameter
795+
param2: Second parameter
796+
Returns:
797+
- First return value
798+
- Second return value
799+
Examples:
800+
- Example 1
801+
- Example 2
802+
"""
803+
pass
804+
805+
description, parameters, returns = get_function_description(test_func)
806+
assert description == "This is a test function."
807+
assert parameters == {"param1": "First parameter", "param2": "Second parameter"}
808+
assert returns == [
809+
"- First return value",
810+
"- Second return value",
811+
"Examples:",
812+
"- Example 1",
813+
"- Example 2",
814+
]
789815

790816
def test_function_with_no_docstring(self):
791817
def test_func():
@@ -835,7 +861,7 @@ def test_func(param1, param2):
835861
description, parameters, returns = get_function_description(test_func)
836862
assert description == "This is a test function."
837863
assert parameters == {"param1": "First parameter", "param2": "Second parameter"}
838-
assert returns == ["First return value", "Second return value"]
864+
assert returns == ["x: First return value", "y: Second return value"]
839865

840866
def test_function_with_multiline_description(self):
841867
def test_func(param1, param2):
@@ -874,7 +900,7 @@ def test_func(param1, param2, param3):
874900
"param2": "",
875901
"param3": "description3",
876902
}
877-
assert returns == ["First return value", "Second return value"]
903+
assert returns == ["- First return value", "- Second return value"]
878904

879905
def test_function_with_nested_colons(self):
880906
def test_func(param1, param2):

0 commit comments

Comments
 (0)