Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
feat: update langchain instrumentation (#952)
Ishan Jain1f07f3cd54ab
diff --git a/sdk/python/pyproject.toml b/sdk/python/pyproject.tomlindex d6cb95e2..702550f8 100644--- a/sdk/python/pyproject.toml+++ b/sdk/python/pyproject.toml@@ -1,6 +1,6 @@[tool.poetry]name = "openlit"-version = "1.36.2"+version = "1.36.3"description = "OpenTelemetry-native Auto instrumentation library for monitoring LLM Applications and GPUs, facilitating the integration of observability into your GenAI-driven projects"authors = ["OpenLIT"]license = "Apache-2.0"diff --git a/sdk/python/src/openlit/_instrumentors.py b/sdk/python/src/openlit/_instrumentors.pyindex e2fdc3ca..826e4ae4 100644--- a/sdk/python/src/openlit/_instrumentors.py+++ b/sdk/python/src/openlit/_instrumentors.py@@ -20,8 +20,7 @@ MODULE_NAME_MAP = {"vllm": "vllm","google-ai-studio": "google.genai","azure-ai-inference": "azure.ai.inference",- "langchain": "langchain",- "langchain_community": "langchain_community",+ "langchain": "langchain_core","llama_index": "llama_index","haystack": "haystack","mem0": "mem0",@@ -88,7 +87,6 @@ INSTRUMENTOR_MAP = {"google-ai-studio": "openlit.instrumentation.google_ai_studio.GoogleAIStudioInstrumentor","azure-ai-inference": "openlit.instrumentation.azure_ai_inference.AzureAIInferenceInstrumentor","langchain": "openlit.instrumentation.langchain.LangChainInstrumentor",- "langchain_community": "openlit.instrumentation.langchain_community.LangChainCommunityInstrumentor","llama_index": "openlit.instrumentation.llamaindex.LlamaIndexInstrumentor","haystack": "openlit.instrumentation.haystack.HaystackInstrumentor","mem0": "openlit.instrumentation.mem0.Mem0Instrumentor",diff --git a/sdk/python/src/openlit/instrumentation/langchain/__init__.py b/sdk/python/src/openlit/instrumentation/langchain/__init__.pyindex 126db071..042707de 100644--- a/sdk/python/src/openlit/instrumentation/langchain/__init__.py+++ b/sdk/python/src/openlit/instrumentation/langchain/__init__.py@@ -1,124 +1,1498 @@"""-OpenLIT LangChain Instrumentation - Callback-Based + Function Wrapping--Provides comprehensive instrumentation for LangChain through:-1. Callback-based hierarchical spans (automatic parent-child relationships)-2. Function wrapping for direct operation instrumentation (Runnables, Embeddings, Tools, etc.)+OpenLIT LangChain Instrumentation"""-from typing import Collection+import logging+import json+import re+import time+from typing import Any, Collection, Dict, List, Optional, Literal, cast+from uuid import UUIDimport importlib.metadata+from opentelemetry.instrumentation.instrumentor import BaseInstrumentor+from opentelemetry.trace import SpanKind, Status, StatusCode++# context_api not needed - callbacks execute in different contexts+from opentelemetry.trace import set_span_in_contextfrom wrapt import wrap_function_wrapper-from openlit.instrumentation.langchain.callback_handler import (- OpenLITLangChainCallbackHandler,-)+# Initialize logger+logger = logging.getLogger(__name__)-_instruments = ("langchain >= 0.1.20",)+_instruments = ("langchain-core >= 0.1.20",)-class CallbackManagerWrapper: # pylint: disable=too-few-public-methods- """Wrapper to inject OpenLIT callback handler into LangChain's callback system"""+class SpanHolder:+ """Container for span and related metadata with performance optimization via __slots__."""- def __init__(self, callback_handler: OpenLITLangChainCallbackHandler):- self.callback_handler = callback_handler+ __slots__ = (+ "span",+ "token",+ "start_time",+ "children",+ "workflow_name",+ "entity_name",+ "streaming_content",+ "input_tokens",+ "first_token_time",+ "token_timestamps",+ "model_name",+ "model_parameters",+ "prompt_content",+ )- def __call__(self, wrapped, instance, args, kwargs):- """Inject OpenLIT callback handler when BaseCallbackManager is initialized"""+ def __init__(self, span, token=None, start_time=None):+ self.span = span+ self.token = token+ self.start_time = start_time or time.time()+ self.children: List[UUID] = []+ self.workflow_name = ""+ self.entity_name = ""+ self.streaming_content: List[str] = []+ self.input_tokens = 0+ self.first_token_time: Optional[float] = None+ self.token_timestamps: List[float] = []+ self.model_name = "unknown"+ self.model_parameters: Dict[str, Any] = {}+ self.prompt_content = ""- # Call original initialization- wrapped(*args, **kwargs)+ def get_duration(self) -> float:+ """Calculate duration from start time to now."""+ return time.time() - self.start_time++ def is_streaming(self) -> bool:+ """Check if this is a streaming response."""+ return len(self.streaming_content) > 0+++# =============================================================================+# Model Name Extraction (from Langfuse)+# =============================================================================- # Check if our callback handler is already registered- for handler in instance.inheritable_handlers:- if isinstance(handler, type(self.callback_handler)):- break+# Known model paths for specific providers+MODEL_PATHS_BY_ID = [+ ("ChatGoogleGenerativeAI", ["kwargs", "model"], "serialized"),+ ("ChatVertexAI", ["kwargs", "model_name"], "serialized"),+ # Mistral+ ("ChatMistralAI", ["kwargs", "model"], "serialized"),+ # OpenAI+ ("OpenAI", ["invocation_params", "model_name"], "kwargs"),+ ("ChatOpenAI", ["invocation_params", "model_name"], "kwargs"),+ ("AzureChatOpenAI", ["invocation_params", "model"], "kwargs"),+ ("AzureChatOpenAI", ["invocation_params", "model_name"], "kwargs"),+ ("AzureChatOpenAI", ["invocation_params", "azure_deployment"], "kwargs"),+ # HuggingFace+ ("HuggingFacePipeline", ["invocation_params", "model_id"], "kwargs"),+ # Bedrock+ ("BedrockChat", ["kwargs", "model_id"], "serialized"),+ ("Bedrock", ["kwargs", "model_id"], "serialized"),+ ("BedrockLLM", ["kwargs", "model_id"], "serialized"),+ ("ChatBedrock", ["kwargs", "model_id"], "serialized"),+ ("ChatBedrockConverse", ["kwargs", "model_id"], "serialized"),+ # Other+ ("LlamaCpp", ["invocation_params", "model_path"], "kwargs"),+ ("WatsonxLLM", ["invocation_params", "model_id"], "kwargs"),+]++# Regex patterns for models where name is in repr string+MODEL_PATTERNS = [+ ("ChatAnthropic", "model", "anthropic"),+ ("Anthropic", "model", "anthropic"),+ ("ChatTongyi", "model_name", None),+ ("ChatCohere", "model", None),+ ("Cohere", "model", None),+ ("HuggingFaceHub", "model", None),+ ("ChatAnyscale", "model_name", None),+ ("TextGen", "model", "text-gen"),+ ("Ollama", "model", None),+ ("OllamaLLM", "model", None),+ ("ChatOllama", "model", None),+ ("ChatFireworks", "model", None),+ ("ChatPerplexity", "model", None),+ ("VLLM", "model", None),+ ("Xinference", "model_uid", None),+ ("ChatOCIGenAI", "model_id", None),+ ("DeepInfra", "model_id", None),+]++# Fallback paths to try+FALLBACK_PATHS = [+ (["kwargs", "model_name"], "serialized"),+ (["kwargs", "model"], "serialized"),+ (["kwargs", "model_id"], "serialized"),+ (["invocation_params", "model_name"], "kwargs"),+ (["invocation_params", "model"], "kwargs"),+ (["invocation_params", "model_id"], "kwargs"),+]+++def _extract_by_path(+ serialized: Optional[Dict[str, Any]],+ kwargs: Dict[str, Any],+ keys: List[str],+ select_from: Literal["serialized", "kwargs"],+) -> Optional[str]:+ """Extract value from nested dict by path."""+ current_obj = kwargs if select_from == "kwargs" else serialized+ if current_obj is None:+ return None++ for key in keys:+ if isinstance(current_obj, dict):+ current_obj = current_obj.get(key)else:- # Add our callback handler to the manager- instance.add_handler(self.callback_handler, True)+ return None+ if current_obj is None:+ return None++ return str(current_obj) if current_obj else None+++def _get_class_name(serialized: Optional[Dict[str, Any]]) -> Optional[str]:+ """Get class name from serialized id."""+ if not serialized:+ return None+ serialized_id = serialized.get("id")+ if serialized_id and isinstance(serialized_id, list) and len(serialized_id) > 0:+ return serialized_id[-1]+ return None+++def _extract_model_from_repr(+ serialized: Optional[Dict[str, Any]], pattern: str+) -> Optional[str]:+ """Extract model name from repr string using regex."""+ if not serialized:+ return None+ repr_str = serialized.get("repr", "")+ if repr_str:+ match = re.search(rf"{pattern}='(.*?)'", repr_str)+ if match:+ return match.group(1)+ return None+++def extract_model_name(+ serialized: Optional[Dict[str, Any]],+ kwargs: Optional[Dict[str, Any]] = None,+) -> str:+ """+ Comprehensive model name extraction (based on Langfuse's approach).++ Tries multiple strategies:+ 1. Known model paths for specific providers+ 2. Regex patterns from repr strings+ 3. Fallback paths+ """+ if kwargs is None:+ kwargs = {}++ class_name = _get_class_name(serialized)++ # 1. Try known model paths+ for model_id, keys, select_from in MODEL_PATHS_BY_ID:+ if class_name == model_id:+ result = _extract_by_path(+ serialized,+ kwargs,+ keys,+ cast(Literal["serialized", "kwargs"], select_from),+ )+ if result:+ return result++ # 2. Try regex patterns+ for model_id, pattern, default in MODEL_PATTERNS:+ if class_name == model_id:+ result = _extract_model_from_repr(serialized, pattern)+ if result:+ return result+ if default:+ return default++ # 3. Try fallback paths+ for keys, select_from in FALLBACK_PATHS:+ result = _extract_by_path(+ serialized, kwargs, keys, cast(Literal["serialized", "kwargs"], select_from)+ )+ if result:+ return result++ # 4. Return class name or unknown+ return class_name or "unknown"+++def extract_model_parameters(kwargs: Dict[str, Any]) -> Dict[str, Any]:+ """+ Extract model parameters from invocation_params (based on Langfuse's approach).+ """+ params = {}+ invocation_params = kwargs.get("invocation_params", {})++ param_keys = [+ "temperature",+ "max_tokens",+ "max_completion_tokens",+ "top_p",+ "top_k",+ "frequency_penalty",+ "presence_penalty",+ "request_timeout",+ "stop_sequences",+ "seed",+ ]++ for key in param_keys:+ value = invocation_params.get(key)+ if value is not None:+ params[key] = value++ return params+++# =============================================================================+# Provider Detection+# =============================================================================++PROVIDER_MAP = {+ "anthropic": "anthropic",+ "azure": "azure",+ "bedrock": "aws.bedrock",+ "bedrock_converse": "aws.bedrock",+ "cohere": "cohere",+ "google": "google",+ "google_genai": "google",+ "google_vertexai": "google",+ "groq": "groq",+ "mistralai": "mistral_ai",+ "ollama": "ollama",+ "openai": "openai",+ "together": "together",+ "vertexai": "google",+ "fireworks": "fireworks",+ "perplexity": "perplexity",+ "huggingface": "huggingface",+ "deepinfra": "deepinfra",+ "anyscale": "anyscale",+}+++def detect_provider(serialized: Optional[Dict[str, Any]]) -> str:+ """Detect the LLM provider from serialized class info."""+ from openlit.semcov import SemanticConvention++ if not serialized:+ return SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN++ class_id = serialized.get("id", [])+ if isinstance(class_id, list):+ class_path = ".".join(class_id).lower()+ for provider, system in PROVIDER_MAP.items():+ if provider in class_path:+ return system++ return SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN+++def detect_observation_type(+ serialized: Optional[Dict[str, Any]], callback_type: str, name: str = ""+) -> str:+ """+ Detect observation type (based on Langfuse's approach).+ Distinguishes agents from regular chains.+ """+ if callback_type == "tool":+ return "tool"+ elif callback_type == "retriever":+ return "retriever"+ elif callback_type == "llm":+ return "generation"+ elif callback_type == "chain":+ # Detect if it's an agent+ if serialized and "id" in serialized:+ class_path = serialized["id"]+ if any("agent" in str(part).lower() for part in class_path):+ return "agent"++ # Check name for agent keywords+ if name and "agent" in name.lower():+ return "agent"++ return "chain"++ return "span"+++# =============================================================================+# Callback Handler+# =============================================================================+++def _create_callback_handler_class(+ tracer,+ version,+ environment,+ application_name,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,+):+ """+ Create and return an OpenLITCallbackHandler class configured with the given parameters.+ """+ try:+ from langchain_core.callbacks import BaseCallbackHandler+ from langchain_core.messages import BaseMessage+ from langchain_core.outputs import LLMResult+ except ImportError:+ logger.debug("langchain_core not available")+ return None++ from openlit.__helpers import (+ general_tokens,+ get_chat_model_cost,+ record_completion_metrics,+ record_framework_metrics,+ calculate_ttft,+ calculate_tbt,+ )+ from openlit.semcov import SemanticConvention++ class OpenLITCallbackHandler(BaseCallbackHandler):+ """+ OpenLIT LangChain Callback Handler with comprehensive instrumentation.+ """++ def __init__(self) -> None:+ super().__init__()+ self._tracer = tracer+ self._version = version+ self._environment = environment+ self._application_name = application_name+ self._pricing_info = pricing_info+ self._capture_message_content = capture_message_content+ self._metrics = metrics+ self._disable_metrics = disable_metrics+ self.spans: Dict[UUID, SpanHolder] = {}+ self.run_inline = True++ def _get_name_from_callback(+ self,+ serialized: Optional[Dict[str, Any]],+ **kwargs: Any,+ ) -> str:+ """Get the name to be used for the span."""+ if kwargs.get("name"):+ return kwargs["name"]+ if serialized:+ if "kwargs" in serialized and serialized["kwargs"].get("name"):+ return serialized["kwargs"]["name"]+ if serialized.get("name"):+ return serialized["name"]+ if "id" in serialized:+ class_id = serialized["id"]+ if isinstance(class_id, list) and len(class_id) > 0:+ return class_id[-1]+ return "unknown"++ def _create_span(+ self,+ run_id: UUID,+ parent_run_id: Optional[UUID],+ span_name: str,+ kind: SpanKind = SpanKind.INTERNAL,+ ) -> Any:+ """Create a new span with proper parent relationship."""+ # For callback-based instrumentation, we manage parent-child relationships+ # via the parent_context parameter, not via context attachment.+ # This avoids "Failed to detach context" errors in streaming scenarios+ # where callbacks may execute in different execution contexts.+ parent_context = None+ if parent_run_id is not None and parent_run_id in self.spans:+ parent_span = self.spans[parent_run_id].span+ parent_context = set_span_in_context(parent_span)++ span = self._tracer.start_span(+ span_name,+ context=parent_context,+ kind=kind,+ )++ # Don't attach to context - callbacks can execute in different contexts+ holder = SpanHolder(span, token=None)+ self.spans[run_id] = holder++ if parent_run_id is not None and parent_run_id in self.spans:+ self.spans[parent_run_id].children.append(run_id)++ return span++ def _end_span(self, run_id: UUID, error: Optional[str] = None) -> None:+ """End a span and clean up."""+ if run_id not in self.spans:+ return++ holder = self.spans[run_id]+ span = holder.span++ # End any child spans that are still open+ for child_id in holder.children:+ if child_id in self.spans:+ child_holder = self.spans[child_id]+ if child_holder.span.end_time is None:+ child_holder.span.end()++ if error:+ span.set_status(Status(StatusCode.ERROR, error))+ else:+ span.set_status(Status(StatusCode.OK))++ span.end()++ del self.spans[run_id]++ def _set_common_attributes(self, span, operation_type: str) -> None:+ """Set common attributes on a span."""+ span.set_attribute(SemanticConvention.GEN_AI_OPERATION, operation_type)+ span.set_attribute(SemanticConvention.GEN_AI_ENVIRONMENT, self._environment)+ span.set_attribute(+ SemanticConvention.GEN_AI_APPLICATION_NAME, self._application_name+ )+ span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, self._version)++ def _extract_from_generations(+ self,+ response,+ input_tokens: int,+ output_tokens: int,+ completion_content: str,+ ) -> tuple:+ """+ Extract token usage and content from response.generations.+ Based on Langfuse's _parse_usage approach for comprehensive extraction.+ """+ if not response.generations:+ return input_tokens, output_tokens, completion_content++ for gen_list in response.generations:+ for gen in gen_list:+ # Extract content+ gen_content = self._extract_generation_content(gen)+ if gen_content and (+ not completion_content+ or len(gen_content) > len(completion_content)+ ):+ completion_content = gen_content++ # Extract usage from message+ tokens = self._extract_message_usage(+ gen, input_tokens, output_tokens+ )+ input_tokens, output_tokens = tokens++ # Check generation_info as fallback+ if hasattr(gen, "generation_info") and gen.generation_info:+ gen_info = gen.generation_info+ if "usage" in gen_info:+ usage = gen_info["usage"]+ input_tokens = usage.get("input_tokens", input_tokens)+ output_tokens = usage.get("output_tokens", output_tokens)++ return input_tokens, output_tokens, completion_content++ def _extract_generation_content(self, gen) -> Optional[str]:+ """Extract content from a generation object."""+ if hasattr(gen, "text") and gen.text:+ return gen.text+ if hasattr(gen, "message") and gen.message:+ msg = gen.message+ if hasattr(msg, "content") and msg.content:+ return (+ msg.content+ if isinstance(msg.content, str)+ else str(msg.content)+ )+ return None++ def _join_streaming_content(self, streaming_content: List[Any]) -> str:+ """+ Join streaming content into a single string.+ Handles nested lists and ensures all items are properly converted.+ """+ if not streaming_content:+ return ""++ str_parts = []+ for item in streaming_content:+ if isinstance(item, str) and item:+ str_parts.append(item)+ elif isinstance(item, list):+ str_parts.extend(self._flatten_list_item(item))+ elif item:+ str_parts.append(str(item))++ return "".join(str_parts) if str_parts else ""++ def _flatten_list_item(self, items: List[Any]) -> List[str]:+ """Flatten a list of items into strings."""+ result = []+ for sub in items:+ if isinstance(sub, str) and sub:+ result.append(sub)+ elif sub:+ result.append(str(sub))+ return result++ def _extract_message_usage(+ self, gen, input_tokens: int, output_tokens: int+ ) -> tuple:+ """+ Extract token usage from message metadata.+ Handles multiple formats based on Langfuse's comprehensive approach:+ - usage_metadata (standard LangChain)+ - response_metadata.usage (Bedrock-Anthropic)+ - response_metadata.amazon-bedrock-invocationMetrics (Bedrock-Titan)+ """+ if not hasattr(gen, "message") or not gen.message:+ return input_tokens, output_tokens++ msg = gen.message++ # Check usage_metadata (standard LangChain, Ollama)+ if hasattr(msg, "usage_metadata") and msg.usage_metadata:+ usage = msg.usage_metadata+ input_tokens = (+ usage.get("input_tokens")+ or usage.get("prompt_tokens")+ or input_tokens+ )+ output_tokens = (+ usage.get("output_tokens")+ or usage.get("completion_tokens")+ or output_tokens+ )++ # Check response_metadata (Bedrock, etc.)+ if hasattr(msg, "response_metadata") and msg.response_metadata:+ metadata = msg.response_metadata+ if isinstance(metadata, dict):+ # Bedrock-Anthropic style+ if "usage" in metadata:+ usage = metadata["usage"]+ input_tokens = usage.get(+ "inputTokens", usage.get("input_tokens", input_tokens)+ )+ output_tokens = usage.get(+ "outputTokens", usage.get("output_tokens", output_tokens)+ )++ # Bedrock-Titan style (amazon-bedrock-invocationMetrics)+ if "amazon-bedrock-invocationMetrics" in metadata:+ metrics = metadata["amazon-bedrock-invocationMetrics"]+ input_tokens = metrics.get("inputTokenCount", input_tokens)+ output_tokens = metrics.get("outputTokenCount", output_tokens)++ return input_tokens, output_tokens++ def _set_model_parameters(self, span, params: Dict[str, Any]) -> None:+ """Set model parameters as span attributes."""+ if params.get("temperature") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_TEMPERATURE, params["temperature"]+ )+ if params.get("max_tokens") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_MAX_TOKENS, params["max_tokens"]+ )+ if params.get("top_p") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_TOP_P, params["top_p"]+ )+ if params.get("top_k") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_TOP_K, params["top_k"]+ )+ if params.get("frequency_penalty") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_FREQUENCY_PENALTY,+ params["frequency_penalty"],+ )+ if params.get("presence_penalty") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_PRESENCE_PENALTY,+ params["presence_penalty"],+ )+ if params.get("seed") is not None:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_SEED, params["seed"]+ )++ # =================================================================+ # Chain Callbacks+ # =================================================================++ def on_chain_start(+ self,+ serialized: Dict[str, Any],+ inputs: Dict[str, Any],+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ tags: Optional[List[str]] = None,+ metadata: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ """Run when chain starts."""+ try:+ name = self._get_name_from_callback(serialized, **kwargs)+ obs_type = detect_observation_type(serialized, "chain", name=name)++ # Use different operation type for agents vs chains+ if obs_type == "agent":+ operation_type = SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT+ span_name = f"agent {name}"+ else:+ operation_type = SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK+ span_name = f"workflow {name}"++ span = self._create_span(run_id, parent_run_id, span_name)++ span.set_attribute(+ SemanticConvention.GEN_AI_SYSTEM,+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ )+ self._set_common_attributes(span, operation_type)+ span.set_attribute(SemanticConvention.GEN_AI_WORKFLOW_TYPE, name)++ if self._capture_message_content and inputs:+ try:+ input_str = json.dumps(inputs, default=str)[:5000]+ except Exception:+ input_str = str(inputs)[:5000]+ span.set_attribute(+ SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str+ )++ except Exception as e:+ logger.debug("Error in on_chain_start: %s", e)++ def on_chain_end(+ self,+ outputs: Dict[str, Any],+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when chain ends."""+ try:+ if run_id not in self.spans:+ return++ holder = self.spans[run_id]+ span = holder.span+ duration = time.time() - holder.start_time++ span.set_attribute(+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration+ )++ if self._capture_message_content and outputs:+ try:+ output_str = json.dumps(outputs, default=str)[:5000]+ except Exception:+ output_str = str(outputs)[:5000]+ span.set_attribute(+ SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, output_str+ )++ # Record metrics+ if not self._disable_metrics and self._metrics:+ try:+ record_framework_metrics(+ metrics=self._metrics,+ gen_ai_operation=SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,+ gen_ai_system=SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ server_address="localhost",+ server_port=8080,+ environment=self._environment,+ application_name=self._application_name,+ start_time=holder.start_time,+ end_time=time.time(),+ )+ except Exception as e:+ logger.debug("Error recording chain metrics: %s", e)++ self._end_span(run_id)++ except Exception as e:+ logger.debug("Error in on_chain_end: %s", e)++ def on_chain_error(+ self,+ error: BaseException,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when chain errors."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ span.set_attribute(+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,+ str(error)[:1000],+ )+ self._end_span(run_id, str(error))+ except Exception as e:+ logger.debug("Error in on_chain_error: %s", e)++ # =================================================================+ # LLM Callbacks+ # =================================================================++ def on_llm_start(+ self,+ serialized: Dict[str, Any],+ prompts: List[str],+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ tags: Optional[List[str]] = None,+ metadata: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ """Run when LLM starts."""+ try:+ model_name = extract_model_name(serialized, kwargs)+ model_params = extract_model_parameters(kwargs)+ provider = detect_provider(serialized)+ span_name = f"chat {model_name}"++ span = self._create_span(+ run_id, parent_run_id, span_name, SpanKind.CLIENT+ )++ span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, provider)+ self._set_common_attributes(+ span, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT+ )+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model_name)+ self._set_model_parameters(span, model_params)++ # Store model info for later+ self.spans[run_id].model_name = model_name+ self.spans[run_id].model_parameters = model_params++ if self._capture_message_content and prompts:+ prompt_str = "\n".join(prompts)[:5000]+ span.set_attribute(+ SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt_str+ )+ self.spans[run_id].input_tokens = general_tokens(prompt_str)++ except Exception as e:+ logger.debug("Error in on_llm_start: %s", e)++ def on_chat_model_start(+ self,+ serialized: Dict[str, Any],+ messages: List[List[BaseMessage]],+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ tags: Optional[List[str]] = None,+ metadata: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ """Run when Chat Model starts."""+ try:+ model_name = extract_model_name(serialized, kwargs)+ model_params = extract_model_parameters(kwargs)+ provider = detect_provider(serialized)+ span_name = f"chat {model_name}"++ span = self._create_span(+ run_id, parent_run_id, span_name, SpanKind.CLIENT+ )++ span.set_attribute(SemanticConvention.GEN_AI_SYSTEM, provider)+ self._set_common_attributes(+ span, SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT+ )+ span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model_name)+ self._set_model_parameters(span, model_params)++ # Store model info+ self.spans[run_id].model_name = model_name+ self.spans[run_id].model_parameters = model_params++ # Always calculate prompt content for token estimation+ if messages:+ formatted = []+ for msg_list in messages:+ for msg in msg_list:+ role = getattr(msg, "type", "unknown")+ content = getattr(msg, "content", str(msg))+ formatted.append(f"{role}: {content}")+ prompt_str = "\n".join(formatted)[:5000]++ # Store prompt for token estimation+ self.spans[run_id].prompt_content = prompt_str+ self.spans[run_id].input_tokens = general_tokens(prompt_str)++ # Set prompt attribute if capturing content+ if self._capture_message_content:+ span.set_attribute(+ SemanticConvention.GEN_AI_CONTENT_PROMPT, prompt_str+ )++ except Exception as e:+ logger.debug("Error in on_chat_model_start: %s", e)++ def on_llm_new_token(+ self,+ token: str,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ chunk: Any = None,+ **kwargs: Any,+ ) -> None:+ """Run on new LLM token (streaming) - tracks TTFT."""+ try:+ if run_id in self.spans:+ holder = self.spans[run_id]+ current_time = time.time()++ # Track first token time for TTFT+ if holder.first_token_time is None:+ holder.first_token_time = current_time++ # Track timestamps for TBT calculation+ holder.token_timestamps.append(current_time)++ # Helper to extract string from content (handles list, str, etc.)+ def extract_str(content):+ if content is None:+ return None+ if isinstance(content, str):+ return content if content else None+ if isinstance(content, list):+ # Flatten list of content items+ parts = []+ for item in content:+ if isinstance(item, str):+ parts.append(item)+ elif isinstance(item, dict) and "text" in item:+ parts.append(str(item["text"]))+ elif hasattr(item, "text"):+ parts.append(str(item.text))+ else:+ parts.append(str(item))+ return "".join(parts) if parts else None+ return str(content)++ # Append the token content+ token_str = extract_str(token)+ if token_str:+ holder.streaming_content.append(token_str)++ # Also try to get content from chunk if available+ # Some providers (Bedrock, etc.) pass content in chunk+ if not token_str and chunk is not None:+ chunk_content = None+ if hasattr(chunk, "content") and chunk.content:+ chunk_content = extract_str(chunk.content)+ elif hasattr(chunk, "text") and chunk.text:+ chunk_content = extract_str(chunk.text)+ elif hasattr(chunk, "message"):+ msg = chunk.message+ if hasattr(msg, "content") and msg.content:+ chunk_content = extract_str(msg.content)++ if chunk_content:+ holder.streaming_content.append(chunk_content)++ except Exception as e:+ logger.debug("Error in on_llm_new_token: %s", e)++ def on_llm_end(+ self,+ response: LLMResult,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when LLM ends."""+ try:+ if run_id not in self.spans:+ return++ holder = self.spans[run_id]+ span = holder.span+ end_time = time.time()+ duration = end_time - holder.start_time++ span.set_attribute(+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration+ )++ # Calculate TTFT and TBT+ ttft = 0.0+ tbt = 0.0+ is_streaming = len(holder.streaming_content) > 0++ if is_streaming:+ ttft = calculate_ttft(holder.token_timestamps, holder.start_time)+ tbt = calculate_tbt(holder.token_timestamps)+ span.set_attribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft)+ span.set_attribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt)+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_IS_STREAM, True+ )+ else:+ span.set_attribute(+ SemanticConvention.GEN_AI_REQUEST_IS_STREAM, False+ )++ # Extract response content+ input_tokens = holder.input_tokens+ output_tokens = 0+ completion_content = ""+ model_name = holder.model_name++ # Get completion from streaming content first+ completion_content = self._join_streaming_content(+ holder.streaming_content+ )++ # Extract from response.generations (works for streaming and non-streaming)+ input_tokens, output_tokens, completion_content = (+ self._extract_from_generations(+ response, input_tokens, output_tokens, completion_content+ )+ )++ # Try to get token usage from llm_output as another fallback+ if response.llm_output:+ token_usage = response.llm_output.get(+ "token_usage"+ ) or response.llm_output.get("usage", {})+ if token_usage:+ input_tokens = (+ token_usage.get("prompt_tokens")+ or token_usage.get("input_tokens")+ or input_tokens+ )+ output_tokens = (+ token_usage.get("completion_tokens")+ or token_usage.get("output_tokens")+ or output_tokens+ )++ # Calculate tokens from content if not available from metadata+ # (Similar to OpenAI instrumentation approach)+ try:+ if output_tokens == 0 and completion_content:+ output_tokens = general_tokens(completion_content)+ except Exception as token_err:+ logger.debug("Error calculating output tokens: %s", token_err)++ # If we still don't have input tokens, estimate from prompt+ try:+ if (+ input_tokens == 0+ and hasattr(holder, "prompt_content")+ and holder.prompt_content+ ):+ input_tokens = general_tokens(holder.prompt_content)+ except Exception as input_err:+ logger.debug("Error calculating input tokens: %s", input_err)++ # Set completion content attribute+ if completion_content and self._capture_message_content:+ span.set_attribute(+ SemanticConvention.GEN_AI_CONTENT_COMPLETION,+ completion_content[:5000],+ )++ # Always set token attributes+ span.set_attribute(+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens+ )+ span.set_attribute(+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens+ )+ span.set_attribute(+ SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS,+ input_tokens + output_tokens,+ )++ # Calculate cost+ cost = get_chat_model_cost(+ model_name, self._pricing_info, input_tokens, output_tokens+ )+ span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)++ # Set response model+ response_model = model_name+ if response.llm_output:+ response_model = response.llm_output.get(+ "model_name"+ ) or response.llm_output.get("model", model_name)+ span.set_attribute(+ SemanticConvention.GEN_AI_RESPONSE_MODEL, response_model+ )++ # Record metrics (like OpenAI instrumentation)+ if not self._disable_metrics and self._metrics:+ try:+ record_completion_metrics(+ metrics=self._metrics,+ gen_ai_operation=SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,+ gen_ai_system=SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ server_address="localhost",+ server_port=8080,+ request_model=model_name,+ response_model=response_model,+ environment=self._environment,+ application_name=self._application_name,+ start_time=holder.start_time,+ end_time=end_time,+ input_tokens=input_tokens,+ output_tokens=output_tokens,+ cost=cost,+ tbt=tbt,+ ttft=ttft,+ )+ except Exception as e:+ logger.debug("Error recording LLM metrics: %s", e)++ self._end_span(run_id)++ except Exception as e:+ logger.debug("Error in on_llm_end: %s", e)++ def on_llm_error(+ self,+ error: BaseException,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when LLM errors."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ span.set_attribute(+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,+ str(error)[:1000],+ )+ self._end_span(run_id, str(error))+ except Exception as e:+ logger.debug("Error in on_llm_error: %s", e)++ # =================================================================+ # Tool Callbacks+ # =================================================================++ def on_tool_start(+ self,+ serialized: Dict[str, Any],+ input_str: str,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ tags: Optional[List[str]] = None,+ metadata: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ """Run when tool starts."""+ try:+ name = self._get_name_from_callback(serialized, **kwargs)+ span_name = f"tool {name}"++ span = self._create_span(run_id, parent_run_id, span_name)++ span.set_attribute(+ SemanticConvention.GEN_AI_SYSTEM,+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ )+ self._set_common_attributes(+ span, SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS+ )+ span.set_attribute(SemanticConvention.GEN_AI_TOOL_NAME, name)++ if self._capture_message_content and input_str:+ span.set_attribute(+ SemanticConvention.GEN_AI_TOOL_INPUT, str(input_str)[:5000]+ )++ except Exception as e:+ logger.debug("Error in on_tool_start: %s", e)++ def on_tool_end(+ self,+ output: str,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when tool ends."""+ try:+ if run_id not in self.spans:+ return++ holder = self.spans[run_id]+ span = holder.span+ duration = time.time() - holder.start_time++ span.set_attribute(+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration+ )++ if self._capture_message_content and output:+ span.set_attribute(+ SemanticConvention.GEN_AI_TOOL_OUTPUT, str(output)[:5000]+ )++ # Record metrics+ if not self._disable_metrics and self._metrics:+ try:+ record_framework_metrics(+ metrics=self._metrics,+ gen_ai_operation=SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS,+ gen_ai_system=SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ server_address="localhost",+ server_port=8080,+ environment=self._environment,+ application_name=self._application_name,+ start_time=holder.start_time,+ end_time=time.time(),+ )+ except Exception as e:+ logger.debug("Error recording tool metrics: %s", e)++ self._end_span(run_id)++ except Exception as e:+ logger.debug("Error in on_tool_end: %s", e)++ def on_tool_error(+ self,+ error: BaseException,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when tool errors."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ span.set_attribute(+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,+ str(error)[:1000],+ )+ self._end_span(run_id, str(error))+ except Exception as e:+ logger.debug("Error in on_tool_error: %s", e)++ # =================================================================+ # Retriever Callbacks+ # =================================================================++ def on_retriever_start(+ self,+ serialized: Dict[str, Any],+ query: str,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ tags: Optional[List[str]] = None,+ metadata: Optional[Dict[str, Any]] = None,+ **kwargs: Any,+ ) -> None:+ """Run when retriever starts."""+ try:+ name = self._get_name_from_callback(serialized, **kwargs)+ span_name = f"retrieval {name}"++ span = self._create_span(run_id, parent_run_id, span_name)++ span.set_attribute(+ SemanticConvention.GEN_AI_SYSTEM,+ SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ )+ self._set_common_attributes(+ span, SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE+ )++ if self._capture_message_content and query:+ span.set_attribute(+ SemanticConvention.GEN_AI_RETRIEVAL_QUERY, str(query)[:5000]+ )++ except Exception as e:+ logger.debug("Error in on_retriever_start: %s", e)++ def on_retriever_end(+ self,+ documents: List[Any],+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when retriever ends."""+ try:+ if run_id not in self.spans:+ return++ holder = self.spans[run_id]+ span = holder.span+ duration = time.time() - holder.start_time++ span.set_attribute(+ SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration+ )++ span.set_attribute(+ SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENT_COUNT, len(documents)+ )++ if self._capture_message_content and documents:+ sample_docs = []+ for doc in documents[:3]:+ content = getattr(doc, "page_content", str(doc))+ sample_docs.append(content[:500])+ span.set_attribute(+ SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENTS,+ "; ".join(sample_docs),+ )++ # Record metrics+ if not self._disable_metrics and self._metrics:+ try:+ record_framework_metrics(+ metrics=self._metrics,+ gen_ai_operation=SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,+ gen_ai_system=SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,+ server_address="localhost",+ server_port=8080,+ environment=self._environment,+ application_name=self._application_name,+ start_time=holder.start_time,+ end_time=time.time(),+ )+ except Exception as e:+ logger.debug("Error recording retriever metrics: %s", e)++ self._end_span(run_id)++ except Exception as e:+ logger.debug("Error in on_retriever_end: %s", e)++ def on_retriever_error(+ self,+ error: BaseException,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run when retriever errors."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ span.set_attribute(+ SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,+ str(error)[:1000],+ )+ self._end_span(run_id, str(error))+ except Exception as e:+ logger.debug("Error in on_retriever_error: %s", e)++ # =================================================================+ # Agent Callbacks+ # =================================================================++ def on_agent_action(+ self,+ action: Any,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run on agent action."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ span.set_attribute(+ SemanticConvention.GEN_AI_OPERATION,+ SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,+ )+ if self._capture_message_content:+ # Extract tool and input from action+ tool = getattr(action, "tool", str(action))+ tool_input = getattr(action, "tool_input", "")+ log = getattr(action, "log", "")+ span.set_attribute(+ SemanticConvention.GEN_AI_AGENT_ACTION_TOOL,+ str(tool)[:1000],+ )+ span.set_attribute(+ SemanticConvention.GEN_AI_AGENT_ACTION_TOOL_INPUT,+ str(tool_input)[:5000],+ )+ if log:+ span.set_attribute(+ SemanticConvention.GEN_AI_AGENT_ACTION_LOG,+ str(log)[:5000],+ )+ except Exception as e:+ logger.debug("Error in on_agent_action: %s", e)++ def on_agent_finish(+ self,+ finish: Any,+ *,+ run_id: UUID,+ parent_run_id: Optional[UUID] = None,+ **kwargs: Any,+ ) -> None:+ """Run on agent finish."""+ try:+ if run_id in self.spans:+ span = self.spans[run_id].span+ if self._capture_message_content:+ # Extract output and log from finish+ output = getattr(finish, "return_values", str(finish))+ log = getattr(finish, "log", "")+ span.set_attribute(+ SemanticConvention.GEN_AI_AGENT_FINISH_OUTPUT,+ str(output)[:5000],+ )+ if log:+ span.set_attribute(+ SemanticConvention.GEN_AI_AGENT_FINISH_LOG,+ str(log)[:5000],+ )+ except Exception as e:+ logger.debug("Error in on_agent_finish: %s", e)++ return OpenLITCallbackHandler+++# =============================================================================+# Callback Manager Wrapper (OpenLLMetry approach)+# =============================================================================+++class _BaseCallbackManagerInitWrapper:+ """+ Wrapper to inject OpenLIT callback handler via BaseCallbackManager.__init__.++ This is the same approach used by OpenLLMetry (Traceloop).+ """++ def __init__(self, callback_handler):+ self._callback_handler = callback_handler++ def get_handler(self):+ """Return the callback handler instance."""+ return self._callback_handler++ def is_handler_present(self, handlers) -> bool:+ """Check if OpenLIT handler is already present in handlers list."""+ return any(isinstance(h, type(self._callback_handler)) for h in handlers)++ def __call__(self, wrapped, instance, args, kwargs) -> None:+ wrapped(*args, **kwargs)++ try:+ if self.is_handler_present(instance.inheritable_handlers):+ return++ instance.add_handler(self._callback_handler, True)+ except Exception as e:+ logger.debug("Error adding callback handler: %s", e)+++# =============================================================================+# Instrumentor+# =============================================================================class LangChainInstrumentor(BaseInstrumentor):"""- OpenLIT LangChain instrumentor using callback-based hierarchical span creation.+ OpenLIT LangChain instrumentor with comprehensive features.- This approach hooks into LangChain's built-in callback system to create- proper parent-child span relationships automatically, providing superior- observability with OpenLIT's comprehensive business intelligence.+ Features from:+ - OpenLLMetry: Auto-injection via BaseCallbackManager.__init__+ - Langfuse: Model name extraction, parameter tracking, agent detection+ - OpenInference: Thread-safe span tracking"""def instrumentation_dependencies(self) -> Collection[str]:return _instrumentsdef _instrument(self, **kwargs):- version = importlib.metadata.version("langchain")+ """Instrument LangChain."""+ try:+ version = importlib.metadata.version("langchain-core")+ except importlib.metadata.PackageNotFoundError:+ version = "unknown"+environment = kwargs.get("environment", "default")application_name = kwargs.get("application_name", "default")tracer = kwargs.get("tracer")pricing_info = kwargs.get("pricing_info", {})capture_message_content = kwargs.get("capture_message_content", False)metrics = kwargs.get("metrics_dict")- disable_metrics = kwargs.get("disable_metrics")-- # Create OpenLIT callback handler with all configuration- openlit_callback_handler = OpenLITLangChainCallbackHandler(- tracer=tracer,- version=version,- environment=environment,- application_name=application_name,- pricing_info=pricing_info,- capture_message_content=capture_message_content,- metrics=metrics,- disable_metrics=disable_metrics,+ disable_metrics = kwargs.get("disable_metrics", False)++ handler_class = _create_callback_handler_class(+ tracer,+ version,+ environment,+ application_name,+ pricing_info,+ capture_message_content,+ metrics,+ disable_metrics,)- # ==================================================================- # PART 1: Hook into LangChain's callback system- # ==================================================================- # This automatically provides hierarchical spans for:- # - RunnableSequence (workflow spans)- # - PromptTemplate (task spans)- # - ChatOpenAI (chat spans)- # - Tools, Retrievers, etc.+ if handler_class is None:+ logger.debug("Could not create LangChain callback handler class")+ return++ handler_instance = handler_class()+try:wrap_function_wrapper(- module="langchain_core.callbacks",+ module="langchain_core.callbacks.manager",name="BaseCallbackManager.__init__",- wrapper=CallbackManagerWrapper(openlit_callback_handler),+ wrapper=_BaseCallbackManagerInitWrapper(handler_instance),)- except Exception:- # Graceful degradation if callback system unavailable- pass-- # ==================================================================- # PART 2: Wrap core LangChain operations for enhanced instrumentation- # ==================================================================- # Note: Function wrapping is currently disabled to keep implementation simple- # and rely on the comprehensive callback system which provides excellent coverage.- # The callback system already instruments:- # - All chain operations (on_chain_start/end)- # - All LLM calls (on_llm_start/end, on_chat_model_start)- # - All tool executions (on_tool_start/end/error)- # - All retriever operations (on_retriever_start/end/error)- # - All agent actions (on_agent_action/finish)- # - Retry events, custom events, and text output- #- # If additional direct instrumentation is needed in the future,- # function wrappers can be added here for operations like:- # - Runnable.invoke() for direct runnable execution- # - Embeddings.embed_documents() for embedding operations- # - BaseCache.lookup() for cache monitoring- # - BaseLoader.load() for document loading-- # Result: Comprehensive observability through callback system+ logger.debug("Successfully wrapped BaseCallbackManager.__init__")+ except Exception as e:+ logger.debug("Failed to wrap BaseCallbackManager.__init__: %s", e)def _uninstrument(self, **kwargs):- """Remove instrumentation"""+ """Remove instrumentation."""try:from opentelemetry.instrumentation.utils import unwrap- # Unwrap callback system- unwrap("langchain_core.callbacks", "BaseCallbackManager.__init__")- except Exception:- pass+ unwrap("langchain_core.callbacks.manager", "BaseCallbackManager.__init__")+ except Exception as e:+ logger.debug("Error during uninstrumentation: %s", e)diff --git a/sdk/python/src/openlit/instrumentation/langchain/callback_handler.py b/sdk/python/src/openlit/instrumentation/langchain/callback_handler.pydeleted file mode 100644index bd7020f0..00000000--- a/sdk/python/src/openlit/instrumentation/langchain/callback_handler.py+++ /dev/null@@ -1,1452 +0,0 @@-"""-OpenLIT LangChain Callback Handler for Hierarchical Span Creation-"""--import time-import json-from datetime import datetime, timezone-from typing import Any, Dict, List, Optional-from uuid import UUID--from langchain_core.callbacks import BaseCallbackHandler-from langchain_core.messages import BaseMessage-from langchain_core.outputs import LLMResult, ChatGeneration, Generation--from opentelemetry import context as context_api-from opentelemetry.trace import SpanKind, set_span_in_context, Status, StatusCode-from opentelemetry.trace.span import Span--from openlit.__helpers import (- common_framework_span_attributes,- handle_exception,- get_chat_model_cost,- general_tokens,-)-from openlit.semcov import SemanticConvention--# Enhanced Provider Mapping (inspired by OpenInference)-LANGCHAIN_PROVIDER_MAP = {- "anthropic": "anthropic",- "azure": "azure",- "azure_ai": "azure",- "azure_openai": "azure",- "bedrock": "aws",- "bedrock_converse": "aws",- "cohere": "cohere",- "deepseek": "deepseek",- "fireworks": "fireworks",- "google": "google",- "google_anthropic_vertex": "google",- "google_genai": "google",- "google_vertexai": "google",- "groq": "groq",- "huggingface": "huggingface",- "ibm": "ibm",- "mistralai": "mistralai",- "nvidia": "nvidia",- "ollama": "ollama",- "openai": "openai",- "perplexity": "perplexity",- "together": "together",- "vertex": "google",- "vertexai": "google",- "xai": "xai",-}---class SpanHolder: # pylint: disable=too-few-public-methods- """Holds span information and tracks relationships"""-- def __init__(self, span: Span, start_time: float, context_token=None):- self.span = span- self.start_time = start_time- self.children: List[UUID] = []- self.workflow_name = ""- self.entity_name = ""- self.context_token = context_token # For context restoration---class OpenLITLangChainCallbackHandler(BaseCallbackHandler):- """- Enhanced OpenLIT callback handler- """-- def __init__(- self,- tracer,- version,- environment,- application_name,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- ):- super().__init__()- self.tracer = tracer- self.version = version- self.environment = environment- self.application_name = application_name- self.pricing_info = pricing_info- self.capture_message_content = capture_message_content- self.metrics = metrics- self.disable_metrics = disable_metrics-- # Track active spans by run_id- self.spans: Dict[UUID, SpanHolder] = {}-- # Track streaming responses by run_id- self.streaming_chunks: Dict[UUID, List[str]] = {}-- self.session_name = environment # Map environment to session- self.tags_enabled = True # Enable tagging system- self.events_enabled = True # Enable events tracking- self.performance_baselines = {} # Store performance baselines- self.error_classification_enabled = True # Enable error classification-- # Required BaseCallbackHandler properties- @property- def raise_error(self) -> bool:- """Should the handler raise errors instead of logging them."""- return False-- @property- def run_inline(self) -> bool:- """Should the handler run inline with the main thread."""- return True-- # Ignore flags - all set to False so we capture everything- @property- def ignore_llm(self) -> bool:- return False-- @property- def ignore_chain(self) -> bool:- return False-- @property- def ignore_agent(self) -> bool:- return False-- @property- def ignore_retriever(self) -> bool:- return False-- @property- def ignore_chat_model(self) -> bool:- return False-- def _get_span_name(self, serialized: Dict[str, Any], operation_type: str) -> str:- """Generate OpenLIT-style span names following our naming convention"""-- # Handle None serialized (common for RunnableSequence)- if not serialized:- return f"{operation_type} RunnableSequence"-- # Extract class name for component identification- if "id" in serialized and serialized["id"]:- component_name = serialized["id"][-1] # Last part is usually the class name- elif "name" in serialized:- component_name = serialized["name"]- else:- component_name = "unknown"-- # Follow OpenLIT naming: {operation_type} {component_name}- return f"{operation_type} {component_name}"-- def _create_span(- self,- run_id: UUID,- parent_run_id: Optional[UUID],- span_name: str,- kind: SpanKind = SpanKind.CLIENT,- model_name: Optional[str] = None,- ) -> Span:- """Create a span with proper parent-child relationship and set as active context"""-- # If we have a parent, create child span in parent context- if parent_run_id and parent_run_id in self.spans:- parent_span = self.spans[parent_run_id].span- span = self.tracer.start_span(- span_name, context=set_span_in_context(parent_span), kind=kind- )- # Track parent-child relationship- self.spans[parent_run_id].children.append(run_id)- else:- # Create root span- span = self.tracer.start_span(span_name, kind=kind)-- # Set this span as the active context so downstream instrumentations (like OpenAI)- # will create child spans under it- span_context = set_span_in_context(span)- context_token = context_api.attach(span_context)-- # Store span with start time and context token- start_time = time.time()- self.spans[run_id] = SpanHolder(span, start_time, context_token)-- # Set common framework span attributes for consistency- scope = type("GenericScope", (), {})()- scope._span = span- scope._start_time = start_time- scope._end_time = None-- # Create mock instance with model name for common_framework_span_attributes- mock_instance = None- if model_name:- mock_instance = type("MockInstance", (), {"model_name": model_name})()-- common_framework_span_attributes(- scope,- SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,- "localhost", # Default server address for LangChain- 8080, # Default port- self.environment,- self.application_name,- self.version,- span_name,- mock_instance,- )-- return span-- def _end_span(self, run_id: UUID) -> None:- """End span and all its children, restore context"""- if run_id not in self.spans:- return-- span_holder = self.spans[run_id]- span = span_holder.span-- # End all child spans first- for child_id in span_holder.children:- if child_id in self.spans:- self._end_span(child_id)-- # Restore the previous context before ending span- if span_holder.context_token:- context_api.detach(span_holder.context_token)-- # Update end time for duration calculation- end_time = time.time()- duration = end_time - span_holder.start_time- span.set_attribute(- SemanticConvention.GEN_AI_CLIENT_OPERATION_DURATION, duration- )-- # End this span- span.set_status(Status(StatusCode.OK))- span.end()-- # Clean up- del self.spans[run_id]-- def _add_langsmith_events(- self, span: Span, event_type: str, data: Optional[Dict] = None- ):- """Add LangSmith-style events to spans"""- if not self.events_enabled:- return-- try:- event_data = {- "timestamp": datetime.now(timezone.utc).isoformat(),- "event_type": event_type,- "session_name": self.session_name,- }-- if data:- event_data.update(data)-- span.add_event(f"langchain.{event_type}", event_data)- except Exception:- pass # Graceful degradation-- def _classify_error(self, error: Exception) -> str:- """Classify errors like LangSmith does"""- error_type = type(error).__name__-- # LangSmith-style error classification- if "rate" in str(error).lower() or "429" in str(error):- return "RATE_LIMIT_ERROR"- elif "timeout" in str(error).lower():- return "TIMEOUT_ERROR"- elif "auth" in str(error).lower() or "401" in str(error):- return "AUTH_ERROR"- elif "not found" in str(error).lower() or "404" in str(error):- return "NOT_FOUND_ERROR"- elif "connection" in str(error).lower():- return "CONNECTION_ERROR"- elif "validation" in str(error).lower():- return "VALIDATION_ERROR"- else:- return f"GENERAL_ERROR_{error_type}"-- def _add_tags_from_context(self, span: Span, run_id: UUID, **kwargs):- """Add LangSmith-style tags to spans"""- if not self.tags_enabled:- return-- try:- tags = []-- # Auto-generate tags based on context- if "model" in kwargs:- tags.append(f"model:{kwargs['model']}")- if "temperature" in kwargs:- tags.append(f"temperature:{kwargs['temperature']}")- if hasattr(kwargs.get("invocation_params", {}), "stream"):- tags.append(- f"streaming:{kwargs['invocation_params'].get('stream', False)}"- )-- # Add session tag- tags.append(f"session:{self.session_name}")-- # Add environment tag- tags.append(f"env:{self.environment}")-- if tags:- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_TAGS, tags)-- except Exception:- pass # Graceful degradation-- def _track_performance_baseline(- self, span: Span, operation_name: str, duration_ms: float- ):- """Track performance against baselines like LangSmith"""- try:- # Store baseline if not exists- if operation_name not in self.performance_baselines:- self.performance_baselines[operation_name] = {- "avg_duration": duration_ms,- "min_duration": duration_ms,- "max_duration": duration_ms,- "count": 1,- }- else:- baseline = self.performance_baselines[operation_name]- baseline["count"] += 1- baseline["avg_duration"] = (- baseline["avg_duration"] * (baseline["count"] - 1) + duration_ms- ) / baseline["count"]- baseline["min_duration"] = min(baseline["min_duration"], duration_ms)- baseline["max_duration"] = max(baseline["max_duration"], duration_ms)-- # Add performance comparison attributes- baseline = self.performance_baselines[operation_name]- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_VS_BASELINE,- duration_ms / baseline["avg_duration"],- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_AVG,- baseline["avg_duration"],- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_PERCENTILE,- _calculate_percentile(duration_ms, baseline),- )-- except Exception:- pass # Graceful degradation-- # Enhanced callback methods with new features- def on_llm_start(- self,- serialized: Dict[str, Any],- prompts: List[str],- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when an LLM starts"""-- try:- # Create workflow span for chain operations- span_name = self._get_span_name(serialized, "workflow")- span = self._create_span(- run_id, parent_run_id, span_name, SpanKind.INTERNAL- )-- # Set OpenLIT attributes- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM,- SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,- )-- # Set workflow-specific attributes- if serialized and "id" in serialized and serialized["id"]:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_TYPE, serialized["id"][-1]- )- else:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_TYPE, "RunnableSequence"- )-- # LangSmith-style serialized function capture- self._capture_serialized_info(span, serialized)-- # Capture input if enabled (with safe JSON serialization)- if self.capture_message_content:- try:- input_str = json.dumps(prompts, default=str)- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str- )- except Exception:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_INPUT, str(prompts)- )-- # Framework enhancements - use attributes only (not events for non-chat operations)-- self._add_tags_from_context(span, run_id, **kwargs)-- # Additional metadata is captured through semantic conventions above-- # Extract provider information (OpenInference-inspired enhancement)- self._extract_provider_info(span, **kwargs)-- except Exception:- # Graceful error handling to prevent callback system failure- pass-- def on_llm_end(- self,- response: LLMResult,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when an LLM call ends"""-- try:- if run_id not in self.spans:- return-- span_holder = self.spans[run_id]- span = span_holder.span-- # If this was a streaming call and we accumulated chunks,- # add the complete streamed response to the span- if run_id in self.streaming_chunks:- complete_response = "".join(self.streaming_chunks[run_id])- if self.capture_message_content and complete_response:- # Set the complete streamed content- span.set_attribute(- SemanticConvention.GEN_AI_CONTENT_COMPLETION, complete_response- )- # Calculate output tokens for the streamed content- output_tokens = general_tokens(complete_response)- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens- )-- # Clean up streaming chunks- del self.streaming_chunks[run_id]-- # Process LLM response with OpenLIT's business intelligence- self._process_llm_response(span, response, run_id)-- # Duration is set in _end_span method-- # NEW: Add performance baseline tracking- if span_holder:- duration_ms = (time.time() - span_holder.start_time) * 1000- self._track_performance_baseline(span, span.name, duration_ms)-- # Framework completion - use attributes only (not events for non-chat operations)-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass-- def on_llm_error(- self,- error: Exception,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when an LLM call ends with an error"""-- try:- if run_id not in self.spans:- return-- span_holder = self.spans[run_id]- span = span_holder.span-- # Clean up streaming chunks if this was a streaming call- if run_id in self.streaming_chunks:- del self.streaming_chunks[run_id]-- # NEW: Enhanced error classification and tracking- # Framework error classification- error_class = self._classify_error(error)- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_CLASS, error_class- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_TYPE, type(error).__name__- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE, str(error)- )-- # Framework error - use attributes only (not events for non-chat operations)-- # Set error status- span.set_status(Status(StatusCode.ERROR, str(error)))- span.record_exception(error)-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass-- def on_chain_start(- self,- serialized: Dict[str, Any],- inputs: Dict[str, Any],- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- tags: Optional[List[str]] = None,- metadata: Optional[Dict[str, Any]] = None,- **kwargs: Any,- ) -> None:- """Called when a chain (RunnableSequence, etc.) starts"""-- try:- # Create workflow span for chain operations- span_name = self._get_span_name(serialized, "workflow")- span = self._create_span(- run_id, parent_run_id, span_name, SpanKind.INTERNAL- )-- # Set OpenLIT attributes- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM,- SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,- )-- # Set workflow-specific attributes- if serialized and "id" in serialized and serialized["id"]:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_TYPE, serialized["id"][-1]- )- else:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_TYPE, "RunnableSequence"- )-- # LangSmith-style serialized function capture- self._capture_serialized_info(span, serialized)-- # Capture input if enabled (with safe JSON serialization)- if self.capture_message_content:- try:- input_str = json.dumps(inputs, default=str)- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str- )- except Exception:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_INPUT, str(inputs)- )-- # Enhanced chain tracking - use attributes only (not events for non-chat operations)-- # Add chain-specific tags- chain_tags = [f"chain_type:{serialized.get('id', ['unknown'])[-1]}"]- if inputs:- chain_tags.append(f"input_count:{len(inputs)}")- span.set_attribute(SemanticConvention.GEN_AI_FRAMEWORK_TAGS, chain_tags)-- except Exception:- # Graceful error handling to prevent callback system failure- pass-- def on_chain_end(- self,- outputs: Dict[str, Any],- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a chain ends"""-- try:- if run_id not in self.spans:- return-- span = self.spans[run_id].span-- # Capture output if enabled (with safe JSON serialization)- if self.capture_message_content:- try:- output_str = json.dumps(outputs, default=str)- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, output_str- )- except Exception:- span.set_attribute(- SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, str(outputs)- )-- # Duration is set in _end_span method-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass-- def on_chain_error(- self,- error: Exception,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a chain ends with an error"""-- try:- if run_id not in self.spans:- return-- span_holder = self.spans[run_id]- span = span_holder.span-- # Clean up streaming chunks if this was a streaming call- if run_id in self.streaming_chunks:- del self.streaming_chunks[run_id]-- # Enhanced error classification and tracking- error_class = self._classify_error(error)- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_CLASS, error_class- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_TYPE, type(error).__name__- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE, str(error)- )-- # Set error status- span.set_status(Status(StatusCode.ERROR, str(error)))- span.record_exception(error)-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass-- def on_chat_model_start(- self,- serialized: Dict[str, Any],- messages: List[List[BaseMessage]],- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- tags: Optional[List[str]] = None,- metadata: Optional[Dict[str, Any]] = None,- **kwargs: Any,- ) -> None:- """Called when a chat model (ChatOpenAI, etc.) starts"""-- try:- # Extract model name from multiple sources with fallback chain- model_name = "unknown"-- # DEBUG: Log serialized data to understand structure- # This helps us improve model extraction logic- # print(f"DEBUG on_chat_model_start: serialized={serialized}")- # print(f"DEBUG on_chat_model_start: kwargs={kwargs}")-- # Try extracting from serialized kwargs first (most reliable)- if (- serialized- and "kwargs" in serialized- ):- # Check for both 'model' and 'model_name' in serialized kwargs- if "model_name" in serialized["kwargs"]:- model_name = serialized["kwargs"]["model_name"]- elif "model" in serialized["kwargs"]:- model_name = serialized["kwargs"]["model"]- elif kwargs.get("invocation_params", {}).get("model"):- # Extract from invocation_params (LangChain's standard location)- model_name = kwargs["invocation_params"]["model"]- elif kwargs.get("invocation_params", {}).get("model_name"):- model_name = kwargs["invocation_params"]["model_name"]- elif kwargs.get("model"):- model_name = kwargs["model"]- elif serialized:- # Try extracting from different parts of serialized data- # LangChain often stores model info in various locations- if "model" in serialized:- model_name = serialized["model"]- elif "model_name" in serialized:- model_name = serialized["model_name"]- elif (- "name" in serialized and "model" in str(serialized["name"]).lower()- ):- model_name = serialized["name"]- elif "id" in serialized and serialized["id"]:- # Extract from class identifier - this gives us the framework being used- class_info = serialized["id"]- if isinstance(class_info, list) and len(class_info) > 0:- class_name = class_info[- -1- ] # Last part is usually the class name- # Infer model based on LangChain provider class- if "chatopenai" in class_name.lower():- model_name = "gpt-3.5-turbo" # Default OpenAI model- elif "chatanthropic" in class_name.lower():- model_name = "claude-3"- elif (- "chatgooglevertexai" in class_name.lower()- or "chatgoogleai" in class_name.lower()- ):- model_name = "gemini-pro"- elif "llama" in class_name.lower():- model_name = "llama-2"- else:- # Use the class name itself as model identifier- model_name = class_name-- # Create chat span with model name- span_name = f"chat {model_name}"- span = self._create_span(- run_id, parent_run_id, span_name, SpanKind.CLIENT, model_name- )-- # Set OpenLIT chat operation attributes- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM,- SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_CHAT,- )- span.set_attribute(SemanticConvention.GEN_AI_REQUEST_MODEL, model_name)-- # Streaming detection- is_streaming = kwargs.get("stream", False) or kwargs.get("streaming", False)- span.set_attribute(- SemanticConvention.GEN_AI_REQUEST_IS_STREAM, is_streaming- )-- # Process messages for content capture and token counting- if messages and len(messages) > 0:- formatted_messages = self._format_messages(messages[0])-- if self.capture_message_content:- span.set_attribute(- SemanticConvention.GEN_AI_CONTENT_PROMPT, formatted_messages- )-- # Calculate input tokens- input_tokens = general_tokens(formatted_messages)- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens- )-- except Exception:- # Graceful error handling- pass-- def on_llm_new_token(- self,- token: str,- *,- chunk: Optional[Any] = None,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a new token is generated during streaming.-- This callback is triggered for each token during astream() operations.- We accumulate the tokens to build the complete response, which will- be processed when on_llm_end() is called.-- Args:- token: The new token generated- chunk: Optional chunk object containing additional metadata- run_id: The run ID for this streaming operation- parent_run_id: The parent run ID if nested- **kwargs: Additional keyword arguments- """- try:- # Initialize streaming chunks list for this run_id if needed- if run_id not in self.streaming_chunks:- self.streaming_chunks[run_id] = []-- # Accumulate the token- if token:- self.streaming_chunks[run_id].append(token)-- # The span remains open - it will be closed in on_llm_end()-- except Exception:- # Graceful error handling to prevent callback system failure- pass-- def _format_messages(self, messages: List[BaseMessage]) -> str:- """Format LangChain messages for content capture"""- formatted = []- for message in messages:- role = self._get_message_role(message)- content = getattr(message, "content", str(message))- formatted.append(f"{role}: {content}")- return "\n".join(formatted)-- def _get_message_role(self, message: BaseMessage) -> str:- """Extract role from LangChain message"""- message_type = message.__class__.__name__.lower()- if "human" in message_type:- return "user"- elif "ai" in message_type:- return "assistant"- elif "system" in message_type:- return "system"- elif "tool" in message_type:- return "tool"- else:- return "user"-- def _process_llm_response(- self, span: Span, response: LLMResult, run_id: UUID- ) -> None:- """Process LLM response with OpenLIT's comprehensive business intelligence"""-- try:- # Extract response content- if response.generations and len(response.generations) > 0:- generation = response.generations[0][0]-- if isinstance(generation, ChatGeneration):- completion_content = generation.message.content- elif isinstance(generation, Generation):- completion_content = generation.text- else:- completion_content = str(generation)-- # Set completion content- if self.capture_message_content:- span.set_attribute(- SemanticConvention.GEN_AI_CONTENT_COMPLETION, completion_content- )-- # Calculate output tokens- output_tokens = general_tokens(completion_content)- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens- )-- # Use enhanced token extraction method (inspired by OpenInference)- self._extract_token_usage(span, response)-- # Extract additional LLM output information- if hasattr(response, "llm_output") and response.llm_output:- llm_output = response.llm_output-- # Model information- if "model_name" in llm_output:- span.set_attribute(- SemanticConvention.GEN_AI_RESPONSE_MODEL,- llm_output["model_name"],- )-- # Calculate cost using OpenLIT's cost tracking- input_tokens = span.attributes.get(- SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 0- )- output_tokens = span.attributes.get(- SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 0- )- model_name = span.attributes.get(- SemanticConvention.GEN_AI_REQUEST_MODEL, ""- )-- if input_tokens and output_tokens and model_name:- cost = get_chat_model_cost(- model_name, self.pricing_info, input_tokens, output_tokens- )- span.set_attribute(SemanticConvention.GEN_AI_USAGE_COST, cost)-- except Exception as e:- handle_exception(span, e)-- def _extract_token_usage(self, span: Span, result: LLMResult) -> None:- """Extract comprehensive token usage with OpenInference-style parsing"""- try:- # Try multiple token usage extraction patterns- token_usage = None-- # Pattern 1: Standard non-streaming (OpenAI, most providers)- if hasattr(result, "llm_output") and result.llm_output:- token_usage = result.llm_output.get(- "token_usage"- ) or result.llm_output.get("usage")-- # Pattern 2: Streaming outputs (when stream_usage=True)- if not token_usage and result.generations:- try:- first_gen = result.generations[0][0]- if hasattr(first_gen, "message") and hasattr(- first_gen.message, "kwargs"- ):- token_usage = first_gen.message.kwargs.get("usage_metadata")- except (IndexError, AttributeError):- pass-- # Pattern 3: VertexAI-specific (generation_info.usage_metadata)- if not token_usage and result.generations:- try:- first_gen = result.generations[0][0]- if (- hasattr(first_gen, "generation_info")- and first_gen.generation_info- ):- token_usage = first_gen.generation_info.get("usage_metadata")- except (IndexError, AttributeError):- pass-- if token_usage:- # Support multiple token field names from different providers- input_tokens = (- token_usage.get("prompt_tokens")- or token_usage.get("input_tokens") # Anthropic- or token_usage.get("prompt_token_count")- ) # Gemini-- output_tokens = (- token_usage.get("completion_tokens")- or token_usage.get("output_tokens") # Anthropic- or token_usage.get("candidates_token_count")- ) # Gemini-- total_tokens = token_usage.get("total_tokens") or token_usage.get(- "total_token_count"- ) # Gemini-- if input_tokens:- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, input_tokens- )- if output_tokens:- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, output_tokens- )- if total_tokens:- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_TOTAL_TOKENS, total_tokens- )-- # Enhanced token details (OpenAI-specific)- if details := token_usage.get("completion_tokens_details"):- if audio_tokens := details.get("audio_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO,- audio_tokens,- )- if reasoning_tokens := details.get("reasoning_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING,- reasoning_tokens,- )-- if details := token_usage.get("prompt_tokens_details"):- if cached_tokens := details.get("cached_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,- cached_tokens,- )- if audio_tokens := details.get("audio_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,- audio_tokens,- )-- # Anthropic cache tokens- if cache_read := token_usage.get("cache_read_input_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ,- cache_read,- )- if cache_write := token_usage.get("cache_creation_input_tokens"):- span.set_attribute(- SemanticConvention.GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE,- cache_write,- )-- except Exception as e:- handle_exception(span, e)-- def _extract_provider_info(self, span: Span, **kwargs) -> None:- """Extract provider information with OpenInference-style mapping"""- try:- # Extract from metadata if available- if "metadata" in kwargs:- metadata = kwargs["metadata"]- if isinstance(metadata, dict) and "ls_provider" in metadata:- provider = metadata["ls_provider"].lower()- mapped_provider = LANGCHAIN_PROVIDER_MAP.get(provider, provider)- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, mapped_provider- )- span.set_attribute(- SemanticConvention.GEN_AI_REQUEST_PROVIDER, mapped_provider- )- return-- # Extract from invocation parameters- if "invocation_params" in kwargs:- params = kwargs["invocation_params"]- if isinstance(params, dict):- # Look for provider indicators in model names- model = params.get("model") or params.get("model_name", "")- if model:- if "gpt-" in model or "o1-" in model:- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, "openai"- )- elif "claude-" in model:- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, "anthropic"- )- elif "gemini-" in model or "bison-" in model:- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, "google"- )- elif "mistral-" in model:- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, "mistralai"- )-- except Exception as e:- handle_exception(span, e)-- def _capture_serialized_info(self, span: Span, serialized: Dict[str, Any]) -> None:- """Capture LangSmith-style serialized function information"""- if not serialized:- return-- try:- # Capture function name (LangSmith enhancement)- if "name" in serialized and serialized["name"]:- span.set_attribute(- SemanticConvention.GEN_AI_SERIALIZED_NAME, serialized["name"]- )-- # Capture function signature if available- if "signature" in serialized and serialized["signature"]:- span.set_attribute(- SemanticConvention.GEN_AI_SERIALIZED_SIGNATURE,- str(serialized["signature"]),- )-- # Capture docstring if available- if "doc" in serialized and serialized["doc"]:- span.set_attribute(- SemanticConvention.GEN_AI_SERIALIZED_DOC,- str(serialized["doc"]),- )-- # Capture module information- if "id" in serialized and isinstance(serialized["id"], list):- module_path = (- ".".join(serialized["id"][:-1]) if len(serialized["id"]) > 1 else ""- )- if module_path:- span.set_attribute(- SemanticConvention.GEN_AI_SERIALIZED_MODULE, module_path- )-- except Exception:- # Graceful failure for serialized info capture- pass-- def on_tool_start(- self,- serialized: Dict[str, Any],- input_str: str,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a tool starts"""-- try:- span_name = self._get_span_name(serialized, "tool")- span = self._create_span(run_id, parent_run_id, span_name, SpanKind.CLIENT)-- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_TOOLS,- )-- if self.capture_message_content and input_str:- span.set_attribute(SemanticConvention.GEN_AI_TOOL_INPUT, str(input_str))- except Exception:- # Graceful error handling- pass-- def on_tool_end(- self,- output: str,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a tool ends"""-- try:- if run_id not in self.spans:- return-- span = self.spans[run_id].span-- if self.capture_message_content and output:- span.set_attribute(SemanticConvention.GEN_AI_TOOL_OUTPUT, str(output))-- # Duration is set in _end_span method-- self._end_span(run_id)- except Exception:- # Graceful error handling- pass-- def on_retriever_start(- self,- serialized: Dict[str, Any],- query: str,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a retriever starts"""-- try:- span_name = self._get_span_name(serialized, "retrieval")- span = self._create_span(run_id, parent_run_id, span_name, SpanKind.CLIENT)-- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_RETRIEVE,- )-- if self.capture_message_content and query:- span.set_attribute(SemanticConvention.GEN_AI_RETRIEVAL_QUERY, str(query))- except Exception:- # Graceful error handling- pass-- def on_retriever_end(- self,- documents,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a retriever ends"""-- if run_id not in self.spans:- return-- span = self.spans[run_id].span-- # Document count- doc_count = len(documents) if documents else 0- span.set_attribute(- SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENT_COUNT, doc_count- )-- # Sample document content- if self.capture_message_content and documents:- sample_docs = []- for doc in documents: # First 3 docs- if hasattr(doc, "page_content"):- sample_docs.append(doc.page_content)- else:- sample_docs.append(str(doc))- span.set_attribute(- SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENTS, "; ".join(sample_docs)- )-- # Duration is set in _end_span method-- self._end_span(run_id)-- def on_agent_action(- self,- action,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when an agent takes an action (selects a tool)"""-- try:- span_name = f"agent_action {getattr(action, 'tool', 'unknown')}"- span = self._create_span(- run_id, parent_run_id, span_name, SpanKind.CLIENT- )-- # Set OpenLIT attributes- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM,- SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN,- )- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_AGENT,- )-- # Capture agent action details- if hasattr(action, "tool"):- span.set_attribute(- SemanticConvention.GEN_AI_AGENT_ACTION_TOOL, action.tool- )-- if hasattr(action, "tool_input") and self.capture_message_content:- tool_input_str = json.dumps(action.tool_input, default=str)- span.set_attribute(- SemanticConvention.GEN_AI_AGENT_ACTION_TOOL_INPUT, tool_input_str- )-- if hasattr(action, "log") and self.capture_message_content:- span.set_attribute(- SemanticConvention.GEN_AI_AGENT_ACTION_LOG, str(action.log)- )-- except Exception:- # Graceful error handling- pass-- def on_agent_finish(- self,- finish,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when an agent finishes"""-- try:- if run_id not in self.spans:- return-- span = self.spans[run_id].span-- # Capture agent finish output- if hasattr(finish, "return_values") and self.capture_message_content:- output_str = json.dumps(finish.return_values, default=str)- span.set_attribute(- SemanticConvention.GEN_AI_AGENT_FINISH_OUTPUT, output_str- )-- if hasattr(finish, "log") and self.capture_message_content:- span.set_attribute(- SemanticConvention.GEN_AI_AGENT_FINISH_LOG, str(finish.log)- )-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass-- def on_text(- self,- text: str,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called on arbitrary text output"""-- try:- # Add text as an event to the current span if it exists- if run_id in self.spans and self.capture_message_content:- span = self.spans[run_id].span- span.add_event(- "text_output",- {- "text": text,- "timestamp": datetime.now(timezone.utc).isoformat(),- },- )-- except Exception:- # Graceful error handling- pass-- def on_retry(- self,- retry_state,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called on retry events"""-- try:- # Add retry event to the current span if it exists- if run_id in self.spans:- span = self.spans[run_id].span- retry_info = {- "timestamp": datetime.now(timezone.utc).isoformat(),- "attempt": getattr(retry_state, "attempt_number", "unknown"),- }-- # Add error information if available- if hasattr(retry_state, "outcome") and retry_state.outcome:- if hasattr(retry_state.outcome, "exception"):- retry_info["error"] = str(- retry_state.outcome.exception()- )-- span.add_event("retry", retry_info)-- except Exception:- # Graceful error handling- pass-- def on_custom_event(- self,- name: str,- data: Any,- *,- run_id: UUID,- tags: Optional[List[str]] = None,- metadata: Optional[Dict[str, Any]] = None,- **kwargs: Any,- ) -> None:- """Called for custom user-defined events"""-- try:- # Add custom event to the current span if it exists- if run_id in self.spans:- span = self.spans[run_id].span-- event_data = {- "name": name,- "timestamp": datetime.now(timezone.utc).isoformat(),- }-- # Add data if capture_message_content is enabled- if self.capture_message_content:- try:- event_data["data"] = json.dumps(data, default=str)- except Exception:- event_data["data"] = str(data)-- # Add tags if provided- if tags:- event_data["tags"] = tags-- # Add metadata if provided- if metadata:- try:- event_data["metadata"] = json.dumps(metadata, default=str)[- :500- ]- except Exception:- event_data["metadata"] = str(metadata)-- span.add_event(f"custom_event.{name}", event_data)-- except Exception:- # Graceful error handling- pass-- def on_tool_error(- self,- error: Exception,- *,- run_id: UUID,- parent_run_id: Optional[UUID] = None,- **kwargs: Any,- ) -> None:- """Called when a tool ends with an error"""-- try:- if run_id not in self.spans:- return-- span_holder = self.spans[run_id]- span = span_holder.span-- # Enhanced error classification and tracking- error_class = self._classify_error(error)- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_CLASS, error_class- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_TYPE, type(error).__name__- )- span.set_attribute(- SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE, str(error)- )-- # Set error status- span.set_status(Status(StatusCode.ERROR, str(error)))- span.record_exception(error)-- self._end_span(run_id)-- except Exception:- # Graceful error handling- pass---def _calculate_percentile(value: float, baseline: Dict[str, Any]) -> float:- """Calculate rough percentile based on min/max"""- try:- min_val = baseline["min_duration"]- max_val = baseline["max_duration"]- if max_val == min_val:- return 50.0- return ((value - min_val) / (max_val - min_val)) * 100- except:- return 50.0 # Default to mediandiff --git a/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py b/sdk/python/src/openlit/instrumentation/langchain_community/__init__.pydeleted file mode 100644index 99b426e7..00000000--- a/sdk/python/src/openlit/instrumentation/langchain_community/__init__.py+++ /dev/null@@ -1,85 +0,0 @@-"""Initializer of Auto Instrumentation of LangChain Community Functions"""--from typing import Collection-import importlib.metadata-from opentelemetry.instrumentation.instrumentor import BaseInstrumentor-from wrapt import wrap_function_wrapper--from openlit.instrumentation.langchain_community.langchain_community import (- general_wrap,-)-from openlit.instrumentation.langchain_community.async_langchain_community import (- async_general_wrap,-)--_instruments = ("langchain-community >= 0.2.0",)--WRAPPED_METHODS = [- {- "package": "langchain_community.document_loaders.base",- "object": "BaseLoader.load",- "endpoint": "langchain_community.retrieve.load",- "wrapper": general_wrap,- },- {- "package": "langchain_community.document_loaders.base",- "object": "BaseLoader.aload",- "endpoint": "langchain_community.retrieve.load",- "wrapper": async_general_wrap,- },- {- "package": "langchain_text_splitters.base",- "object": "TextSplitter.split_documents",- "endpoint": "langchain_community.retrieve.split_documents",- "wrapper": general_wrap,- },- {- "package": "langchain_text_splitters.base",- "object": "TextSplitter.create_documents",- "endpoint": "langchain_community.retrieve.create_documents",- "wrapper": general_wrap,- },-]---class LangChainCommunityInstrumentor(BaseInstrumentor):- """- An instrumentor for LangChain Community client library.- """-- def instrumentation_dependencies(self) -> Collection[str]:- return _instruments-- def _instrument(self, **kwargs):- version = importlib.metadata.version("langchain-community")- environment = kwargs.get("environment", "default")- application_name = kwargs.get("application_name", "default")- tracer = kwargs.get("tracer")- pricing_info = kwargs.get("pricing_info", {})- capture_message_content = kwargs.get("capture_message_content", False)- metrics = kwargs.get("metrics_dict")- disable_metrics = kwargs.get("disable_metrics")-- for wrapped_method in WRAPPED_METHODS:- wrap_package = wrapped_method.get("package")- wrap_object = wrapped_method.get("object")- gen_ai_endpoint = wrapped_method.get("endpoint")- wrapper = wrapped_method.get("wrapper")- wrap_function_wrapper(- wrap_package,- wrap_object,- wrapper(- gen_ai_endpoint,- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,- ),- )-- def _uninstrument(self, **kwargs):- passdiff --git a/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py b/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.pydeleted file mode 100644index b45a26ce..00000000--- a/sdk/python/src/openlit/instrumentation/langchain_community/async_langchain_community.py+++ /dev/null@@ -1,67 +0,0 @@-"""-Module for monitoring async LangChain Community operations.-"""--import logging-from opentelemetry import trace-from opentelemetry.trace import Status, StatusCode--from openlit.__helpers import handle_exception-from openlit.instrumentation.langchain_community.utils import process_general_response--# Initialize logger for LangChain Community instrumentation-logger = logging.getLogger(__name__)---def async_general_wrap(- gen_ai_endpoint,- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,-):- """- Generates a telemetry wrapper for GenAI operations.- """-- async def wrapper(wrapped, instance, args, kwargs):- """- Wraps the GenAI operation call.- """-- # Prepare server address and port- server_address = "127.0.0.1"- server_port = "80"-- # Get the parent span from the tracer- with tracer.start_as_current_span(- gen_ai_endpoint, kind=trace.SpanKind.CLIENT- ) as span:- try:- # Call the original async function- response = await wrapped(*args, **kwargs)-- # Process the response using the utility function- response = process_general_response(- response,- gen_ai_endpoint,- server_port,- server_address,- environment,- application_name,- span,- version,- )-- span.set_status(Status(StatusCode.OK))-- except Exception as e:- handle_exception(span, e)-- return response-- return wrapperdiff --git a/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py b/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.pydeleted file mode 100644index 49d57e05..00000000--- a/sdk/python/src/openlit/instrumentation/langchain_community/langchain_community.py+++ /dev/null@@ -1,67 +0,0 @@-"""-Module for monitoring LangChain Community operations.-"""--import logging-from opentelemetry import trace-from opentelemetry.trace import Status, StatusCode--from openlit.__helpers import handle_exception-from openlit.instrumentation.langchain_community.utils import process_general_response--# Initialize logger for LangChain Community instrumentation-logger = logging.getLogger(__name__)---def general_wrap(- gen_ai_endpoint,- version,- environment,- application_name,- tracer,- pricing_info,- capture_message_content,- metrics,- disable_metrics,-):- """- Generates a telemetry wrapper for GenAI operations.- """-- def wrapper(wrapped, instance, args, kwargs):- """- Wraps the GenAI operation call.- """-- # Prepare server address and port- server_address = "127.0.0.1"- server_port = "80"-- # Get the parent span from the tracer- with tracer.start_as_current_span(- gen_ai_endpoint, kind=trace.SpanKind.CLIENT- ) as span:- try:- # Call the original function- response = wrapped(*args, **kwargs)-- # Process the response using the utility function- response = process_general_response(- response,- gen_ai_endpoint,- server_port,- server_address,- environment,- application_name,- span,- version,- )-- span.set_status(Status(StatusCode.OK))-- except Exception as e:- handle_exception(span, e)-- return response-- return wrapperdiff --git a/sdk/python/src/openlit/instrumentation/langchain_community/utils.py b/sdk/python/src/openlit/instrumentation/langchain_community/utils.pydeleted file mode 100644index 238ba840..00000000--- a/sdk/python/src/openlit/instrumentation/langchain_community/utils.py+++ /dev/null@@ -1,95 +0,0 @@-"""-Utility functions for LangChain Community instrumentation.-"""--from opentelemetry.trace import Status, StatusCode-from openlit.semcov import SemanticConvention---def process_general_response(- response,- gen_ai_endpoint,- server_port,- server_address,- environment,- application_name,- span,- version="1.0.0",-):- """- Process general LangChain Community operations (document loading, text splitting) and generate telemetry.-- Args:- response: The response object from the LangChain Community operation- gen_ai_endpoint: The endpoint identifier for the operation- server_port: Server port (empty for community operations)- server_address: Server address (empty for community operations)- environment: Environment name- application_name: Application name- span: OpenTelemetry span- version: Version string-- Returns:- The original response object- """-- # Set span attributes for general operations- span.set_attribute(- SemanticConvention.GEN_AI_SYSTEM, SemanticConvention.GEN_AI_SYSTEM_LANGCHAIN- )- span.set_attribute(SemanticConvention.GEN_AI_ENDPOINT, gen_ai_endpoint)- span.set_attribute(- SemanticConvention.GEN_AI_OPERATION,- SemanticConvention.GEN_AI_OPERATION_TYPE_FRAMEWORK,- )- span.set_attribute(SemanticConvention.GEN_AI_SDK_VERSION, version)- span.set_attribute(SemanticConvention.GEN_AI_ENVIRONMENT, environment)- span.set_attribute(SemanticConvention.GEN_AI_APPLICATION_NAME, application_name)-- # Try to extract source information for document loading operations- if gen_ai_endpoint and "retrieve.load" in gen_ai_endpoint:- try:- if hasattr(response, "__iter__") and len(response) > 0:- # For document loaders, try to get source from first document- first_doc = response[0]- if hasattr(first_doc, "metadata") and isinstance(- first_doc.metadata, dict- ):- source = first_doc.metadata.get("source", "unknown")- span.set_attribute(- SemanticConvention.GEN_AI_RETRIEVAL_SOURCE, source- )-- # Count number of documents loaded- span.set_attribute("gen_ai.retrieval.documents.count", len(response))- except (AttributeError, KeyError, IndexError, TypeError):- # If we cant extract metadata, just continue without it- pass-- # For text splitting operations- elif gen_ai_endpoint and (- "split_documents" in gen_ai_endpoint or "create_documents" in gen_ai_endpoint- ):- try:- if hasattr(response, "__iter__") and len(response) > 0:- # Count number of text chunks created- span.set_attribute("gen_ai.text_splitter.chunks.count", len(response))-- # Try to get average chunk size- total_chars = sum(- len(doc.page_content)- for doc in response- if hasattr(doc, "page_content")- )- if total_chars > 0:- avg_chunk_size = total_chars // len(response)- span.set_attribute(- "gen_ai.text_splitter.avg_chunk_size", avg_chunk_size- )- except (AttributeError, TypeError):- # If we cant extract chunk information, just continue without it- pass-- span.set_status(Status(StatusCode.OK))-- return responsediff --git a/sdk/python/src/openlit/semcov/__init__.py b/sdk/python/src/openlit/semcov/__init__.pyindex edef182b..fc786afc 100644--- a/sdk/python/src/openlit/semcov/__init__.py+++ b/sdk/python/src/openlit/semcov/__init__.py@@ -325,9 +325,15 @@ class SemanticConvention:GEN_AI_FRAMEWORK_ERROR_CLASS = "gen_ai.framework.error.class"GEN_AI_FRAMEWORK_ERROR_TYPE = "gen_ai.framework.error.type"GEN_AI_FRAMEWORK_ERROR_MESSAGE = "gen_ai.framework.error.message"- GEN_AI_FRAMEWORK_PERFORMANCE_VS_BASELINE = "gen_ai.framework.performance.vs_baseline"- GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_AVG = "gen_ai.framework.performance.baseline_avg"- GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_PERCENTILE = "gen_ai.framework.performance.baseline_percentile"+ GEN_AI_FRAMEWORK_PERFORMANCE_VS_BASELINE = (+ "gen_ai.framework.performance.vs_baseline"+ )+ GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_AVG = (+ "gen_ai.framework.performance.baseline_avg"+ )+ GEN_AI_FRAMEWORK_PERFORMANCE_BASELINE_PERCENTILE = (+ "gen_ai.framework.performance.baseline_percentile"+ )# Serialized Function Capture (LangSmith-style)GEN_AI_SERIALIZED_NAME = "gen_ai.serialized.name"@@ -339,10 +345,18 @@ class SemanticConvention:GEN_AI_REQUEST_PROVIDER = "gen_ai.request.provider"# Enhanced Token Usage Details (OpenAI/Anthropic Cache Support)- GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO = "gen_ai.usage.completion_tokens_details.audio_tokens"- GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING = "gen_ai.usage.completion_tokens_details.reasoning_tokens"- GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ = "gen_ai.usage.prompt_tokens_details.cached_tokens"- GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE = "gen_ai.usage.prompt_tokens_details.cache_creation_tokens"+ GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_AUDIO = (+ "gen_ai.usage.completion_tokens_details.audio_tokens"+ )+ GEN_AI_USAGE_COMPLETION_TOKENS_DETAILS_REASONING = (+ "gen_ai.usage.completion_tokens_details.reasoning_tokens"+ )+ GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_READ = (+ "gen_ai.usage.prompt_tokens_details.cached_tokens"+ )+ GEN_AI_USAGE_PROMPT_TOKENS_DETAILS_CACHE_WRITE = (+ "gen_ai.usage.prompt_tokens_details.cache_creation_tokens"+ )# GenAI RagGEN_AI_RAG_MAX_SEGMENTS = "gen_ai.rag.max_segments"diff --git a/sdk/python/tests/requirements.txt b/sdk/python/tests/requirements.txtindex bfb73d85..6bf95b6a 100644--- a/sdk/python/tests/requirements.txt+++ b/sdk/python/tests/requirements.txt@@ -14,9 +14,6 @@ boto3botocoregoogle-genaiazure-ai-inference-mem0ai-ai21-controlflowassemblyaifirecrawl-pytogether@@ -31,10 +28,6 @@ pineconeqdrant-clientastrapy-# Frameworks-langchain-langchainhub-# GPUxmltodict