Skip to content

Commit 4bf1f5f

Browse files
committed
fix: serialize standard context tools
Guard tool extraction so that when context tools expose a `standard_tools` list we include both their raw form and a sanitized serialized version with name/description tuples; fall back to the previous behavior otherwise.
1 parent b45dcb1 commit 4bf1f5f

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

src/pipecat/utils/tracing/service_decorators.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,10 +995,58 @@ async def wrapper(self, *args, **kwargs):
995995
try:
996996
context_tools = getattr(self._context, "tools", None)
997997
if context_tools and not operation_attrs.get("tools"):
998-
operation_attrs["tools"] = context_tools
999-
operation_attrs["tools_serialized"] = json.dumps(
1000-
context_tools
1001-
)
998+
tools_list = []
999+
tools_serialized = None
1000+
1001+
if hasattr(context_tools, "standard_tools"):
1002+
# ToolsSchema object
1003+
tools_list = context_tools.standard_tools
1004+
tools_serialized = json.dumps(
1005+
[
1006+
{
1007+
"name": tool.name
1008+
if hasattr(tool, "name")
1009+
else tool.get("name", "unknown"),
1010+
"description": tool.description
1011+
if hasattr(tool, "description")
1012+
else tool.get("description", ""),
1013+
"properties": tool.properties
1014+
if hasattr(tool, "properties")
1015+
else tool.get("properties", {}),
1016+
"required": tool.required
1017+
if hasattr(tool, "required")
1018+
else tool.get("required", []),
1019+
}
1020+
for tool in tools_list
1021+
]
1022+
)
1023+
elif isinstance(context_tools, list):
1024+
# List of tool dictionaries or objects
1025+
tools_list = context_tools
1026+
tools_serialized = json.dumps(
1027+
[
1028+
{
1029+
"name": tool.get("name", "unknown")
1030+
if isinstance(tool, dict)
1031+
else getattr(tool, "name", "unknown"),
1032+
"description": tool.get("description", "")
1033+
if isinstance(tool, dict)
1034+
else getattr(tool, "description", ""),
1035+
"properties": tool.get("properties", {})
1036+
if isinstance(tool, dict)
1037+
else getattr(tool, "properties", {}),
1038+
"required": tool.get("required", [])
1039+
if isinstance(tool, dict)
1040+
else getattr(tool, "required", []),
1041+
}
1042+
for tool in tools_list
1043+
]
1044+
)
1045+
1046+
if tools_list:
1047+
operation_attrs["tools"] = tools_list
1048+
operation_attrs["tools_serialized"] = tools_serialized
1049+
10021050
except Exception as e:
10031051
logging.warning(f"Error extracting context tools: {e}")
10041052

0 commit comments

Comments
 (0)