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.pyindex 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_schemaelse:@@ -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 = Noneif "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.pyindex 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_schemafrom letta.functions.helpers import generate_model_from_args_json_schemafrom letta.functions.schema_generator import generate_schema_from_args_schema_v2from letta.log import get_logger+from letta.otel.tracing import trace_methodfrom letta.schemas.enums import ToolSourceType, ToolTypefrom letta.schemas.tool import Tool as PydanticToollogger = get_logger(__name__)+@trace_methoddef 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 jsonschema_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 schemaif 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 jsonschema_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 effortelse: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 jsonschema_size_kb = len(json.dumps(schema)) / 1024