Add 'Invalid Renamed Tool Parameter' Check (#324)
Arcade tools can rename parameters like so,
```py
@tool()
def func_with_renamed_param(
param1: Annotated[str, "MyRenamedParam", "The first parameter"],
):
pass
```
but there is no check for whether the renamed parameter is a valid
identifier.
Anthropic models, for example, will fail if a renamed parameter is not a
valid identifier (which was the cause for
https://github.com/ArcadeAI/arcade-ai/pull/319).
This commit is contained in:
parent
c9ed5ce123
commit
e26f647c3d
2 changed files with 22 additions and 1 deletions
|
|
@ -575,7 +575,14 @@ def extract_field_info(param: inspect.Parameter) -> ToolParamInfo:
|
||||||
elif len(str_annotations) == 1:
|
elif len(str_annotations) == 1:
|
||||||
param_info.description = str_annotations[0]
|
param_info.description = str_annotations[0]
|
||||||
elif len(str_annotations) == 2:
|
elif len(str_annotations) == 2:
|
||||||
param_info.name = str_annotations[0]
|
new_name = str_annotations[0]
|
||||||
|
if not new_name.isidentifier():
|
||||||
|
raise ToolDefinitionError(
|
||||||
|
f"Invalid parameter name: '{new_name}' is not a valid identifier. "
|
||||||
|
"Identifiers must start with a letter or underscore, "
|
||||||
|
"and can only contain letters, digits, or underscores."
|
||||||
|
)
|
||||||
|
param_info.name = new_name
|
||||||
param_info.description = str_annotations[1]
|
param_info.description = str_annotations[1]
|
||||||
else:
|
else:
|
||||||
raise ToolDefinitionError(
|
raise ToolDefinitionError(
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
from typing import Annotated
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from arcade.core.catalog import ToolCatalog
|
from arcade.core.catalog import ToolCatalog
|
||||||
|
|
@ -41,6 +43,13 @@ def func_with_multiple_context_params(context: ToolContext, context2: ToolContex
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@tool(desc="A function with an invalid renamed parameter")
|
||||||
|
def func_with_invalid_renamed_param(
|
||||||
|
param1: Annotated[str, "invalid-param-name", "The first parameter"],
|
||||||
|
):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@tool(
|
@tool(
|
||||||
desc="A function with a required secret with a missing key (illegal)",
|
desc="A function with a required secret with a missing key (illegal)",
|
||||||
requires_secrets=[""],
|
requires_secrets=[""],
|
||||||
|
|
@ -95,6 +104,11 @@ def func_with_secret_requirement_invalid_type():
|
||||||
ToolDefinitionError,
|
ToolDefinitionError,
|
||||||
id=func_with_multiple_context_params.__name__,
|
id=func_with_multiple_context_params.__name__,
|
||||||
),
|
),
|
||||||
|
pytest.param(
|
||||||
|
func_with_invalid_renamed_param,
|
||||||
|
ToolDefinitionError,
|
||||||
|
id=func_with_invalid_renamed_param.__name__,
|
||||||
|
),
|
||||||
pytest.param(
|
pytest.param(
|
||||||
func_with_missing_secret_key,
|
func_with_missing_secret_key,
|
||||||
ToolDefinitionError,
|
ToolDefinitionError,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue