Skip to content

Commit 6f3977f

Browse files
committed
Update tool_schema.py
1 parent 40421c3 commit 6f3977f

File tree

1 file changed

+80
-21
lines changed

1 file changed

+80
-21
lines changed

agent-memory-client/agent_memory_client/tool_schema.py

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from __future__ import annotations
99

10+
from collections.abc import Iterator
1011
from typing import TYPE_CHECKING, Any, Literal
1112

1213
if TYPE_CHECKING:
@@ -60,11 +61,21 @@ def set_description(self, description: str) -> Self:
6061
6162
Returns:
6263
Self for method chaining
64+
65+
Raises:
66+
ValueError: If the schema structure is malformed
6367
"""
64-
if self._format == "openai":
65-
self._schema["function"]["description"] = description
66-
else: # anthropic
67-
self._schema["description"] = description
68+
try:
69+
if self._format == "openai":
70+
self._schema["function"]["description"] = description
71+
else: # anthropic
72+
self._schema["description"] = description
73+
except (KeyError, TypeError) as e:
74+
raise ValueError(
75+
f"Malformed schema structure for {self._format} format. "
76+
f"Expected {'function.description' if self._format == 'openai' else 'description'} path. "
77+
f"Original error: {e}"
78+
) from e
6879
return self
6980

7081
def set_name(self, name: str) -> Self:
@@ -76,11 +87,21 @@ def set_name(self, name: str) -> Self:
7687
7788
Returns:
7889
Self for method chaining
90+
91+
Raises:
92+
ValueError: If the schema structure is malformed
7993
"""
80-
if self._format == "openai":
81-
self._schema["function"]["name"] = name
82-
else: # anthropic
83-
self._schema["name"] = name
94+
try:
95+
if self._format == "openai":
96+
self._schema["function"]["name"] = name
97+
else: # anthropic
98+
self._schema["name"] = name
99+
except (KeyError, TypeError) as e:
100+
raise ValueError(
101+
f"Malformed schema structure for {self._format} format. "
102+
f"Expected {'function.name' if self._format == 'openai' else 'name'} path. "
103+
f"Original error: {e}"
104+
) from e
84105
return self
85106

86107
def set_parameter_description(self, param_name: str, description: str) -> Self:
@@ -108,16 +129,40 @@ def set_parameter_description(self, param_name: str, description: str) -> Self:
108129
return self
109130

110131
def get_description(self) -> str:
111-
"""Get the current tool description."""
112-
if self._format == "openai":
113-
return self._schema["function"]["description"]
114-
return self._schema["description"]
132+
"""
133+
Get the current tool description.
134+
135+
Raises:
136+
ValueError: If the schema structure is malformed
137+
"""
138+
try:
139+
if self._format == "openai":
140+
return self._schema["function"]["description"]
141+
return self._schema["description"]
142+
except (KeyError, TypeError) as e:
143+
raise ValueError(
144+
f"Malformed schema structure for {self._format} format. "
145+
f"Expected {'function.description' if self._format == 'openai' else 'description'} path. "
146+
f"Original error: {e}"
147+
) from e
115148

116149
def get_name(self) -> str:
117-
"""Get the current tool name."""
118-
if self._format == "openai":
119-
return self._schema["function"]["name"]
120-
return self._schema["name"]
150+
"""
151+
Get the current tool name.
152+
153+
Raises:
154+
ValueError: If the schema structure is malformed
155+
"""
156+
try:
157+
if self._format == "openai":
158+
return self._schema["function"]["name"]
159+
return self._schema["name"]
160+
except (KeyError, TypeError) as e:
161+
raise ValueError(
162+
f"Malformed schema structure for {self._format} format. "
163+
f"Expected {'function.name' if self._format == 'openai' else 'name'} path. "
164+
f"Original error: {e}"
165+
) from e
121166

122167
def get_parameter_description(self, param_name: str) -> str | None:
123168
"""
@@ -216,10 +261,17 @@ def set_description(self, name: str, description: str) -> Self:
216261
217262
Returns:
218263
Self for method chaining
264+
265+
Raises:
266+
KeyError: If no tool with the given name exists in the collection
219267
"""
220268
schema = self.get_by_name(name)
221-
if schema:
222-
schema.set_description(description)
269+
if schema is None:
270+
raise KeyError(
271+
f"Tool '{name}' not found in collection. "
272+
f"Available tools: {self.names()}"
273+
)
274+
schema.set_description(description)
223275
return self
224276

225277
def set_name(self, old_name: str, new_name: str) -> Self:
@@ -232,10 +284,17 @@ def set_name(self, old_name: str, new_name: str) -> Self:
232284
233285
Returns:
234286
Self for method chaining
287+
288+
Raises:
289+
KeyError: If no tool with the given name exists in the collection
235290
"""
236291
schema = self.get_by_name(old_name)
237-
if schema:
238-
schema.set_name(new_name)
292+
if schema is None:
293+
raise KeyError(
294+
f"Tool '{old_name}' not found in collection. "
295+
f"Available tools: {self.names()}"
296+
)
297+
schema.set_name(new_name)
239298
return self
240299

241300
def to_list(self) -> list[dict[str, Any]]:
@@ -250,7 +309,7 @@ def names(self) -> list[str]:
250309
"""Get all tool names in the collection."""
251310
return [s.get_name() for s in self._schemas]
252311

253-
def __iter__(self):
312+
def __iter__(self) -> Iterator[ToolSchema]:
254313
return iter(self._schemas)
255314

256315
def __len__(self) -> int:

0 commit comments

Comments
 (0)