Skip to content

Build and run agents you can see, understand and trust.

fix(tool): keep resolvable annotations when one hint fails (#3484)

Meng Chen9cfa26b2867d
diff --git a/src/agentscope/tool/_utils.py b/src/agentscope/tool/_utils.py
index f64046d24..3fd7f656e 100644
--- a/src/agentscope/tool/_utils.py
+++ b/src/agentscope/tool/_utils.py
@@ -61,10 +61,11 @@ def _resolve_annotation(
def _resolve_partial_type_hints(
- partial_func: partial,
+ partial_func: Callable,
globalns: dict[str, Any] | None,
) -> dict[str, Any]:
- """Resolve each annotation on a partial independently."""
+ """Resolve each annotation on a callable (or partial) independently,
+ skipping the ones that cannot be resolved."""
raw_annotations = getattr(
partial_func,
"__annotations__",
@@ -99,15 +100,18 @@ def _resolve_type_hints(tool_func: Callable) -> dict[str, Any]:
partial_funcs.append(annotated_func)
annotated_func = annotated_func.func
+ globalns = getattr(annotated_func, "__globals__", None)
+
try:
type_hints = typing.get_type_hints(
annotated_func,
include_extras=True,
)
except Exception:
- type_hints = {}
-
- globalns = getattr(annotated_func, "__globals__", None)
+ # One unresolvable annotation (e.g. a `TYPE_CHECKING`-only import
+ # used by `_agent_state` or the return type) must not discard the
+ # hints of every other parameter.
+ type_hints = _resolve_partial_type_hints(annotated_func, globalns)
for partial_func in reversed(partial_funcs):
type_hints.update(
diff --git a/tests/tool_lazy_annotations_test.py b/tests/tool_lazy_annotations_test.py
index 8018177f9..dbdba3ccc 100644
--- a/tests/tool_lazy_annotations_test.py
+++ b/tests/tool_lazy_annotations_test.py
@@ -9,7 +9,7 @@ resolved back to real types before being handed to pydantic.
from __future__ import annotations
from functools import partial
-from typing import Annotated
+from typing import TYPE_CHECKING, Annotated
from unittest import TestCase
from pydantic import BaseModel, Field
@@ -17,6 +17,9 @@ from pydantic import BaseModel, Field
from agentscope.tool import FunctionTool
from agentscope.tool._utils import _extract_input_schema
+if TYPE_CHECKING:
+ from agentscope.state import AgentState
+
class Location(BaseModel):
"""A location model used as a parameter type."""
@@ -81,6 +84,14 @@ primitive_parameter_with_unresolvable_return.__annotations__ = {
}
+def state_injected_tool(
+ query: Annotated[str, Field(description="The search query")],
+ _agent_state: AgentState | None = None,
+) -> str:
+ """Search with the injected agent state."""
+ return query
+
+
class LazyAnnotationsTest(TestCase):
"""Schema extraction must work with stringized annotations."""
@@ -403,3 +414,22 @@ class LazyAnnotationsTest(TestCase):
"required": ["value"],
},
)
+
+ def test_type_checking_only_annotation_keeps_other_hints(self) -> None:
+ """A ``TYPE_CHECKING``-only annotation on the injected state does
+ not discard the hints of the other parameters."""
+ tool = FunctionTool(state_injected_tool, is_state_injected=True)
+
+ self.assertDictEqual(
+ tool.input_schema,
+ {
+ "type": "object",
+ "properties": {
+ "query": {
+ "type": "string",
+ "description": "The search query",
+ },
+ },
+ "required": ["query"],
+ },
+ )