Skip to content

Commit aff1065

Browse files
fix: async functions in tool specs (#19000)
1 parent fa00eb7 commit aff1065

File tree

6 files changed

+25
-30
lines changed

6 files changed

+25
-30
lines changed

llama-index-core/llama_index/core/tools/tool_spec/base.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,10 @@ def to_tool_list(
9494
"spec_functions must be of type: List[Union[str, Tuple[str, str]]]"
9595
)
9696

97-
if func_sync is None:
98-
if func_async is not None:
99-
func_sync = patch_sync(func_async)
100-
else:
101-
raise ValueError(
102-
f"Could not retrieve a function for spec: {func_spec}"
103-
)
104-
10597
tool = FunctionTool.from_defaults(
10698
fn=func_sync,
10799
async_fn=func_async,
108100
tool_metadata=metadata,
109101
)
110102
tool_list.append(tool)
111103
return tool_list
112-
113-
114-
def patch_sync(func_async: AsyncCallable) -> Callable:
115-
"""Patch sync function from async function."""
116-
117-
def patched_sync(*args: Any, **kwargs: Any) -> Any:
118-
try:
119-
loop = asyncio.get_event_loop()
120-
except RuntimeError:
121-
loop = asyncio.new_event_loop()
122-
asyncio.set_event_loop(loop)
123-
return loop.run_until_complete(func_async(*args, **kwargs))
124-
125-
return patched_sync

llama-index-core/llama_index/core/vector_stores/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class MetadataFilter(BaseModel):
9595
r"""
9696
Comprehensive metadata filter for vector stores to support more operators.
9797
98-
Value uses Strict* types, as int, float and str are compatible types and were all
98+
Value uses Strict types, as int, float and str are compatible types and were all
9999
converted to string before.
100100
101101
See: https://docs.pydantic.dev/latest/usage/types/#strict-types

llama-index-core/tests/readers/file/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_SimpleDirectoryReader_empty(data_path):
9999

100100
def test_SimpleDirectoryReader_file_limit(data_path):
101101
r = SimpleDirectoryReader(input_dir=data_path, recursive=True, num_files_limit=2)
102-
assert [f.name for f in r.input_files] == ["excluded_0.txt", "file_0.md"]
102+
assert len(r.input_files) == 2
103103

104104

105105
def test_SimpleDirectoryReader_list_resources(data_path):

llama-index-core/tests/tools/tool_spec/test_base.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class TestToolSpec(BaseToolSpec):
2828
"bar",
2929
"abc",
3030
"abc_with_ctx",
31+
"async_only_fn",
3132
]
3233

3334
def foo(self, arg1: str, arg2: int) -> str:
@@ -46,6 +47,10 @@ async def abar(self, arg1: bool) -> str:
4647
"""Abar."""
4748
return self.bar(arg1=arg1)
4849

50+
async def async_only_fn(self) -> str:
51+
"""Async only fn."""
52+
return "async only fn"
53+
4954
def abc(self, arg1: str) -> str:
5055
# NOTE: no docstring
5156
return f"bar {arg1}"
@@ -62,7 +67,7 @@ def test_tool_spec() -> None:
6267
tool_spec = TestToolSpec()
6368
# first is foo, second is bar
6469
tools = tool_spec.to_tool_list()
65-
assert len(tools) == 4
70+
assert len(tools) == 5
6671
assert tools[0].metadata.name == "foo"
6772
assert tools[0].metadata.description == "foo(arg1: str, arg2: int) -> str\nFoo."
6873
assert tools[0].fn("hello", 1) == "foo hello 1"
@@ -101,7 +106,7 @@ def test_tool_spec() -> None:
101106
),
102107
}
103108
)
104-
assert len(tools) == 4
109+
assert len(tools) == 5
105110
assert tools[0].metadata.name == "foo_name"
106111
assert tools[0].metadata.description == "foo_description"
107112
assert tools[0].metadata.fn_schema is not None
@@ -121,19 +126,28 @@ async def test_tool_spec_async() -> None:
121126
"""Test async_fn of tool spec."""
122127
tool_spec = TestToolSpec()
123128
tools = tool_spec.to_tool_list()
124-
assert len(tools) == 4
129+
assert len(tools) == 5
130+
125131
assert await tools[0].async_fn("hello", 1) == "foo hello 1"
126132
assert str(await tools[1].acall(True)) == "bar True"
127133

134+
assert tools[0].fn("hello", 1) == "foo hello 1"
135+
assert str(tools[1](True)) == "bar True"
136+
128137

129138
def test_async_patching() -> None:
130139
# test sync patching of async function
131140
tool_spec = TestToolSpec()
132-
tool_spec.spec_functions = ["afoo"]
141+
tool_spec.spec_functions = ["afoo", "async_only_fn"]
133142
tools = tool_spec.to_tool_list()
134-
assert len(tools) == 1
143+
assert len(tools) == 2
135144
assert tools[0].fn("hello", 1) == "foo hello 1"
136145

146+
assert tools[0].metadata.name == "afoo"
147+
assert tools[0].metadata.description == "afoo(arg1: str, arg2: int) -> str\nAfoo."
148+
assert tools[1].metadata.name == "async_only_fn"
149+
assert tools[1].metadata.description == "async_only_fn() -> str\nAsync only fn."
150+
137151

138152
def test_tool_spec_subset() -> None:
139153
"""Test tool spec subset."""

llama-index-integrations/tools/llama-index-tools-playwright/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ pip install llama-index-tools-playwright
1616
In order to use this tool, you need to have a async Playwright browser instance. You can hook one up by running the following code:
1717

1818
```python
19-
browser = PlaywrightToolSpec.create_async_playwright_browser(headless=False)
19+
browser = await PlaywrightToolSpec.create_async_playwright_browser(
20+
headless=False
21+
)
2022
playwright_tool = PlaywrightToolSpec.from_async_browser(browser)
2123
```
2224

llama-index-packs/llama-index-packs-raptor/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ dependencies = [
4242
"umap-learn>=0.5.5",
4343
"scikit-learn",
4444
"llama-index-core>=0.12.0,<0.13",
45+
"numpy<=2.2",
4546
]
4647

4748
[tool.codespell]

0 commit comments

Comments
 (0)