Skip to content

Commit 1c26afd

Browse files
author
Pierre
committed
clean_docstring
wrap the instructions from the docstring into clean_docstring to clean up the empty lines.
1 parent 88c3805 commit 1c26afd

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

workflowai/core/client/_fn_utils.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ async def __call__(self, input: AgentInput, **kwargs: Unpack[RunParams[AgentOutp
147147
yield chunk.output
148148

149149

150+
def clean_docstring(docstring: Optional[str]) -> str:
151+
"""Clean a docstring by removing empty lines at start/end and normalizing indentation."""
152+
if not docstring:
153+
return ""
154+
155+
# Split into lines and remove empty lines at start/end
156+
lines = [line.rstrip() for line in docstring.split("\n")]
157+
while lines and not lines[0].strip():
158+
lines.pop(0)
159+
while lines and not lines[-1].strip():
160+
lines.pop()
161+
162+
if not lines:
163+
return ""
164+
165+
# Find and remove common indentation
166+
indent = min(len(line) - len(line.lstrip()) for line in lines if line.strip())
167+
lines = [line[indent:] if line.strip() else "" for line in lines]
168+
169+
return "\n".join(lines)
170+
171+
150172
def wrap_run_template(
151173
client: Callable[[], APIClient],
152174
agent_id: str,
@@ -165,7 +187,7 @@ def wrap_run_template(
165187

166188
if not version and (fn.__doc__ or model):
167189
version = VersionProperties(
168-
instructions=fn.__doc__,
190+
instructions=clean_docstring(fn.__doc__),
169191
model=model,
170192
)
171193

workflowai/core/client/_fn_utils_test.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_RunnableStreamAgent, # pyright: ignore [reportPrivateUsage]
1414
_RunnableStreamOutputOnlyAgent, # pyright: ignore [reportPrivateUsage]
1515
agent_wrapper,
16+
clean_docstring,
1617
extract_fn_spec,
1718
get_generic_args,
1819
is_async_iterator,
@@ -113,3 +114,58 @@ async def test_fn_stream_output_only(self, mock_api_client: Mock):
113114
assert len(chunks) == 1
114115
assert isinstance(chunks[0], HelloTaskOutput)
115116
assert chunks[0] == HelloTaskOutput(message="Hello, World!")
117+
118+
119+
def test_clean_docstring():
120+
# Test empty docstring
121+
assert clean_docstring("") == ""
122+
assert clean_docstring(None) == ""
123+
124+
# Test single line docstring
125+
assert clean_docstring("Hello world") == "Hello world"
126+
assert clean_docstring(" Hello world ") == "Hello world"
127+
128+
# Test docstring with empty lines at start/end
129+
assert clean_docstring("""
130+
131+
Hello world
132+
133+
""") == "Hello world"
134+
135+
# Test multi-line docstring with indentation
136+
assert clean_docstring("""
137+
First line
138+
Second line
139+
Indented line
140+
Last line
141+
""") == "First line\nSecond line\n Indented line\nLast line"
142+
143+
# Test docstring with empty lines in between
144+
assert clean_docstring("""
145+
First line
146+
147+
Second line
148+
149+
Third line
150+
""") == "First line\n\nSecond line\n\nThird line"
151+
152+
# Test real-world example
153+
expected = (
154+
"Find the capital city of the country where the input city is located.\n\n"
155+
"Guidelines:\n"
156+
"1. First identify the country where the input city is located\n"
157+
"2. Then provide the capital city of that country\n"
158+
"3. Include an interesting historical or cultural fact about the capital\n"
159+
"4. Be accurate and precise with geographical information\n"
160+
"5. If the input city is itself the capital, still provide the information"
161+
)
162+
assert clean_docstring("""
163+
Find the capital city of the country where the input city is located.
164+
165+
Guidelines:
166+
1. First identify the country where the input city is located
167+
2. Then provide the capital city of that country
168+
3. Include an interesting historical or cultural fact about the capital
169+
4. Be accurate and precise with geographical information
170+
5. If the input city is itself the capital, still provide the information
171+
""") == expected

0 commit comments

Comments
 (0)