Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

chore: add more tracing to tool upsert endpoint (#6471)

Kian Jones964fc05cb954
diff --git a/letta/services/tool_manager.py b/letta/services/tool_manager.py
index 98f5a19ff..0e1a2353e 100644
--- a/letta/services/tool_manager.py
+++ b/letta/services/tool_manager.py
@@ -201,8 +201,11 @@ class ToolManager:
self, pydantic_tool: PydanticTool, actor: PydanticUser, bypass_name_check: bool = False, modal_sandbox_enabled: bool = False
) -> PydanticTool:
"""Create a new tool based on the ToolCreate schema."""
+ from letta.otel.tracing import tracer
+
if pydantic_tool.tool_type == ToolType.CUSTOM and not pydantic_tool.json_schema:
- generated_schema = generate_schema_for_tool_creation(pydantic_tool)
+ with tracer.start_as_current_span("generate_schema_for_tool_creation"):
+ generated_schema = generate_schema_for_tool_creation(pydantic_tool)
if generated_schema:
pydantic_tool.json_schema = generated_schema
else:
@@ -229,7 +232,8 @@ class ToolManager:
current_tool = await self.get_tool_by_name_async(tool_name=pydantic_tool.name, actor=actor)
if current_tool:
# Put to dict and remove fields that should not be reset
- update_data = pydantic_tool.model_dump(exclude_unset=True, exclude_none=True)
+ with tracer.start_as_current_span("pydantic_tool.model_dump"):
+ update_data = pydantic_tool.model_dump(exclude_unset=True, exclude_none=True)
update_data["organization_id"] = actor.organization_id
# If there's anything to update
@@ -239,9 +243,11 @@ class ToolManager:
updated_tool_type = None
if "tool_type" in update_data:
updated_tool_type = update_data.get("tool_type")
+ with tracer.start_as_current_span("ToolUpdate_initialization"):
+ tool_update = ToolUpdate(**update_data)
tool = await self.update_tool_by_id_async(
current_tool.id,
- ToolUpdate(**update_data),
+ tool_update,
actor,
updated_tool_type=updated_tool_type,
modal_sandbox_enabled=modal_sandbox_enabled,
diff --git a/letta/services/tool_schema_generator.py b/letta/services/tool_schema_generator.py
index 575f3183a..d1f821886 100644
--- a/letta/services/tool_schema_generator.py
+++ b/letta/services/tool_schema_generator.py
@@ -7,12 +7,14 @@ from letta.functions.functions import derive_openai_json_schema
from letta.functions.helpers import generate_model_from_args_json_schema
from letta.functions.schema_generator import generate_schema_from_args_schema_v2
from letta.log import get_logger
+from letta.otel.tracing import trace_method
from letta.schemas.enums import ToolSourceType, ToolType
from letta.schemas.tool import Tool as PydanticTool
logger = get_logger(__name__)
+@trace_method
def generate_schema_for_tool_creation(
tool: PydanticTool,
) -> Optional[dict]:
@@ -45,8 +47,10 @@ def generate_schema_for_tool_creation(
if tool.source_type == ToolSourceType.typescript:
try:
from letta.functions.typescript_parser import derive_typescript_json_schema
+ from letta.otel.tracing import tracer
- schema = derive_typescript_json_schema(source_code=tool.source_code)
+ with tracer.start_as_current_span("derive_typescript_json_schema"):
+ schema = derive_typescript_json_schema(source_code=tool.source_code)
import json
schema_size_kb = len(json.dumps(schema)) / 1024
@@ -58,16 +62,21 @@ def generate_schema_for_tool_creation(
# Python tools (default if not specified for backwards compatibility)
elif tool.source_type == ToolSourceType.python or tool.source_type is None:
+ from letta.otel.tracing import tracer
+
# If args_json_schema is provided, use it to generate full schema
if tool.args_json_schema:
- name, description = get_function_name_and_docstring(tool.source_code, tool.name)
- args_schema = generate_model_from_args_json_schema(tool.args_json_schema)
- schema = generate_schema_from_args_schema_v2(
- args_schema=args_schema,
- name=name,
- description=description,
- append_heartbeat=False,
- )
+ with tracer.start_as_current_span("get_function_name_and_docstring"):
+ name, description = get_function_name_and_docstring(tool.source_code, tool.name)
+ with tracer.start_as_current_span("generate_model_from_args_json_schema"):
+ args_schema = generate_model_from_args_json_schema(tool.args_json_schema)
+ with tracer.start_as_current_span("generate_schema_from_args_schema_v2"):
+ schema = generate_schema_from_args_schema_v2(
+ args_schema=args_schema,
+ name=name,
+ description=description,
+ append_heartbeat=False,
+ )
import json
schema_size_kb = len(json.dumps(schema)) / 1024
@@ -76,7 +85,8 @@ def generate_schema_for_tool_creation(
# Otherwise, attempt to parse from docstring with best effort
else:
try:
- schema = derive_openai_json_schema(source_code=tool.source_code)
+ with tracer.start_as_current_span("derive_openai_json_schema"):
+ schema = derive_openai_json_schema(source_code=tool.source_code)
import json
schema_size_kb = len(json.dumps(schema)) / 1024