Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix: make content truncation limits configurable via max_content_length (#1049)

Giulio Leoned157b8070cc0
diff --git a/docs/latest/sdk/configuration.mdx b/docs/latest/sdk/configuration.mdx
index a5257226..6a945d98 100644
--- a/docs/latest/sdk/configuration.mdx
+++ b/docs/latest/sdk/configuration.mdx
@@ -43,6 +43,7 @@ Customize OpenLIT SDK behavior for your specific instrumentation needs:
| `capture_message_content` | `--capture_message_content` | `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` | Enable LLM prompt and response content tracing for debugging | `True` | No |
| `disabled_instrumentors` | `--disabled_instrumentors` | `OPENLIT_DISABLED_INSTRUMENTORS` | Disable specific AI service instrumentations (comma-separated) | `None` | No |
| `disable_metrics` | `--disable_metrics` | `OPENLIT_DISABLE_METRICS` | Disable cost tracking and performance metrics collection | `False` | No |
+| `max_content_length` | `--max_content_length` | `OPENLIT_MAX_CONTENT_LENGTH` | Maximum character length for captured prompt/completion content. `None` (default) means no truncation. Set to a positive integer to truncate content exceeding that length. `0` or `-1` also disable truncation. | `None` | No |
| `pricing_json` | `--pricing_json` | `OPENLIT_PRICING_JSON` | Custom pricing configuration for accurate LLM cost tracking | `None` | No |
| `detailed_tracing` | `--detailed_tracing` | `OPENLIT_DETAILED_TRACING` | Enable detailed AI framework and component-level tracing | `True` | No |
| `collect_system_metrics` | `--collect_system_metrics` | `OPENLIT_COLLECT_SYSTEM_METRICS` | Comprehensive system monitoring (CPU, memory, disk, network, GPU) for AI workloads | `False` | No |
diff --git a/docs/latest/sdk/features/tracing.mdx b/docs/latest/sdk/features/tracing.mdx
index 940d077a..de3f9c39 100644
--- a/docs/latest/sdk/features/tracing.mdx
+++ b/docs/latest/sdk/features/tracing.mdx
@@ -60,6 +60,25 @@ Example:
openlit.init(capture_message_content=False)
```
+## Limit Content Length
+
+By default, OpenLIT captures full prompt and completion content without any truncation. If you want to limit the size of captured content (for example, to reduce trace storage costs or avoid excessively large spans), set `max_content_length` to a positive integer. Content exceeding the limit will be truncated with `...` appended.
+
+A value of `None` (the default), `0`, or `-1` disables truncation entirely.
+
+Example:
+
+```python
+# Truncate all captured content to 500 characters
+openlit.init(max_content_length=500)
+```
+
+You can also set this via the environment variable:
+
+```bash
+export OPENLIT_MAX_CONTENT_LENGTH=500
+```
+
## Disable Batch
By default, the SDK batches spans using the OpenTelemetry batch span processor. When working locally, sometimes you may wish to disable this behavior. You can do that with this flag.
diff --git a/sdk/python/src/openlit/__helpers.py b/sdk/python/src/openlit/__helpers.py
index 8fb7e167..0c8f69dc 100644
--- a/sdk/python/src/openlit/__helpers.py
+++ b/sdk/python/src/openlit/__helpers.py
@@ -19,11 +19,59 @@ from opentelemetry.sdk.resources import (
from opentelemetry.trace import Status, StatusCode
from opentelemetry._logs import LogRecord
from openlit.semcov import SemanticConvention
+from openlit._config import OpenlitConfig
# Set up logging
logger = logging.getLogger(__name__)
+def truncate_content(text):
+ """Return *text* as a string, optionally truncated to ``max_content_length``.
+
+ By default (``max_content_length is None``), no truncation is applied.
+ When ``OpenlitConfig.max_content_length`` is set to a positive integer,
+ the string is truncated to that many characters with ``...`` appended.
+ A value of ``0`` or ``-1`` explicitly disables truncation (same as None).
+ """
+
+ s = str(text) if text is not None else ""
+
+ raw_limit = getattr(OpenlitConfig, "max_content_length", None)
+ if raw_limit is not None:
+ try:
+ limit = int(raw_limit)
+ except (TypeError, ValueError):
+ return s
+ if limit <= 0:
+ return s
+ if len(s) > limit:
+ return s[:limit] + "..."
+ return s
+
+
+def truncate_message_content(messages):
+ """Apply truncation to text content fields within OTel message structures.
+
+ Walks the standard ``[{"role": ..., "parts": [{"type": "text", "content": ...}]}]``
+ structure produced by ``build_input_messages`` / ``build_output_messages`` and
+ applies ``truncate_content`` to every text ``content`` and tool-call ``response``
+ field. Operates in-place and returns *messages* for convenience.
+ """
+ if not isinstance(messages, list):
+ return messages
+ for msg in messages:
+ if not isinstance(msg, dict):
+ continue
+ for part in msg.get("parts", []):
+ if not isinstance(part, dict):
+ continue
+ if "content" in part and isinstance(part["content"], str):
+ part["content"] = truncate_content(part["content"])
+ if "response" in part and isinstance(part["response"], str):
+ part["response"] = truncate_content(part["response"])
+ return messages
+
+
def parse_exporters(env_var_name):
"""
Parse comma-separated exporter names from environment variable.
diff --git a/sdk/python/src/openlit/__init__.py b/sdk/python/src/openlit/__init__.py
index f201cf44..d0d2cd81 100644
--- a/sdk/python/src/openlit/__init__.py
+++ b/sdk/python/src/openlit/__init__.py
@@ -22,6 +22,7 @@ from openlit.otel.tracing import setup_tracing
from openlit.otel.metrics import setup_meter
from openlit.otel.events import setup_events
from openlit.__helpers import fetch_pricing_info, get_env_variable
+from openlit._config import OpenlitConfig # noqa: F401 — re-exported for public API
from openlit._instrumentors import MODULE_NAME_MAP, get_all_instrumentors
# Import GPU instrumentor separately as it doesn't follow the standard pattern
@@ -35,112 +36,6 @@ import openlit.evals
logger = logging.getLogger(__name__)
-class OpenlitConfig:
- """
- A Singleton Configuration class for openLIT.
-
- This class maintains a single instance of configuration settings including
- environment details, application name, and tracing information throughout the openLIT package.
-
- Attributes:
- environment (str): Deployment environment of the application.
- application_name (str): Name of the application using openLIT.
- pricing_info (Dict[str, Any]): Pricing information.
- tracer (Optional[Any]): Tracer instance for OpenTelemetry.
- event_provider (Optional[Any]): Event logger provider for OpenTelemetry.
- otlp_endpoint (Optional[str]): Endpoint for OTLP.
- otlp_headers (Optional[Dict[str, str]]): Headers for OTLP.
- disable_batch (bool): Flag to disable batch span processing in tracing.
- capture_message_content (bool): Flag to enable or disable tracing of content.
- detailed_tracing (bool): Flag to enable detailed component-level tracing.
- """
-
- _instance = None
-
- def __new__(cls):
- """Ensures that only one instance of the configuration exists."""
- if cls._instance is None:
- cls._instance = super(OpenlitConfig, cls).__new__(cls)
- cls.reset_to_defaults()
- return cls._instance
-
- @classmethod
- def reset_to_defaults(cls):
- """Resets configuration to default values."""
- cls.environment = "default"
- cls.application_name = "default"
- cls.pricing_info = {}
- cls.tracer = None
- cls.event_provider = None
- cls.metrics_dict = {}
- cls.otlp_endpoint = None
- cls.otlp_headers = None
- cls.disable_batch = False
- cls.capture_message_content = True
- cls.disable_metrics = False
- cls.detailed_tracing = True
- # Database instrumentation options
- cls.capture_parameters = False
- cls.enable_sqlcommenter = False
- cls.evals_logs_export = True
-
- @classmethod
- def update_config(
- cls,
- environment,
- application_name,
- tracer,
- event_provider,
- otlp_endpoint,
- otlp_headers,
- disable_batch,
- capture_message_content,
- metrics_dict,
- disable_metrics,
- pricing_json,
- detailed_tracing,
- capture_parameters=False,
- enable_sqlcommenter=False,
- evals_logs_export=True,
- ):
- """
- Updates the configuration based on provided parameters.
-
- Args:
- environment (str): Deployment environment.
- application_name (str): Application name.
- tracer: Tracer instance.
- event_provider: Event logger provider instance.
- meter: Metric Instance
- otlp_endpoint (str): OTLP endpoint.
- otlp_headers (Dict[str, str]): OTLP headers.
- disable_batch (bool): Disable batch span processing flag.
- capture_message_content (bool): Enable or disable content tracing.
- metrics_dict: Dictionary of metrics.
- disable_metrics (bool): Flag to disable metrics.
- pricing_json(str): path or url to the pricing json file
- detailed_tracing (bool): Flag to enable detailed component-level tracing.
- capture_parameters (bool): Capture database query parameters (security risk).
- enable_sqlcommenter (bool): Inject trace context as SQL comments.
- evals_logs_export (bool): Emit evaluation results as OTEL Log Records instead of OTEL Events.
- """
- cls.environment = environment
- cls.application_name = application_name
- cls.pricing_info = fetch_pricing_info(pricing_json)
- cls.tracer = tracer
- cls.event_provider = event_provider
- cls.metrics_dict = metrics_dict
- cls.otlp_endpoint = otlp_endpoint
- cls.otlp_headers = otlp_headers
- cls.disable_batch = disable_batch
- cls.capture_message_content = capture_message_content
- cls.disable_metrics = disable_metrics
- cls.detailed_tracing = detailed_tracing
- cls.capture_parameters = capture_parameters
- cls.enable_sqlcommenter = enable_sqlcommenter
- cls.evals_logs_export = evals_logs_export
-
-
def module_exists(module_name):
"""Check if nested modules exist, addressing the dot notation issue."""
parts = module_name.split(".")
@@ -234,6 +129,7 @@ def init(
capture_parameters=False,
enable_sqlcommenter=False,
evals_logs_export=True,
+ max_content_length=None,
):
"""
Initializes the openLIT configuration and setups tracing.
@@ -257,6 +153,9 @@ def init(
collect_gpu_stats (bool): Flag to enable or disable GPU metrics collection.
detailed_tracing (bool): Enable detailed component-level tracing for debugging and optimization.
Defaults to False to use workflow-level tracing with minimal storage overhead.
+ max_content_length (int): Maximum character length for captured content attributes (prompts,
+ completions, tool output, etc.). None (default) means no truncation.
+ Set to a positive integer to truncate content to that length.
"""
disabled_instrumentors = disabled_instrumentors if disabled_instrumentors else []
logger.info("Starting openLIT initialization...")
@@ -315,6 +214,8 @@ def init(
enable_sqlcommenter = env_config["enable_sqlcommenter"]
if evals_logs_export is True and "evals_logs_export" in env_config:
evals_logs_export = env_config["evals_logs_export"]
+ if max_content_length is None and "max_content_length" in env_config:
+ max_content_length = env_config["max_content_length"]
except ImportError:
# Fallback if config module is not available - continue without env var support
@@ -395,11 +296,12 @@ def init(
capture_message_content,
metrics_dict,
disable_metrics,
- pricing_json,
+ fetch_pricing_info(pricing_json),
detailed_tracing,
capture_parameters,
enable_sqlcommenter,
evals_logs_export,
+ max_content_length,
)
# Create instrumentor instances dynamically
diff --git a/sdk/python/src/openlit/_config.py b/sdk/python/src/openlit/_config.py
new file mode 100644
index 00000000..c977718f
--- /dev/null
+++ b/sdk/python/src/openlit/_config.py
@@ -0,0 +1,114 @@
+"""
+Singleton configuration class for OpenLIT.
+
+Extracted into its own module to avoid circular imports between
+``openlit.__init__`` and ``openlit.__helpers``.
+"""
+
+
+class OpenlitConfig:
+ """
+ A Singleton Configuration class for openLIT.
+
+ This class maintains a single instance of configuration settings including
+ environment details, application name, and tracing information throughout the openLIT package.
+
+ Attributes:
+ environment (str): Deployment environment of the application.
+ application_name (str): Name of the application using openLIT.
+ pricing_info (Dict[str, Any]): Pricing information.
+ tracer (Optional[Any]): Tracer instance for OpenTelemetry.
+ event_provider (Optional[Any]): Event logger provider for OpenTelemetry.
+ otlp_endpoint (Optional[str]): Endpoint for OTLP.
+ otlp_headers (Optional[Dict[str, str]]): Headers for OTLP.
+ disable_batch (bool): Flag to disable batch span processing in tracing.
+ capture_message_content (bool): Flag to enable or disable tracing of content.
+ detailed_tracing (bool): Flag to enable detailed component-level tracing.
+ """
+
+ _instance = None
+
+ def __new__(cls):
+ """Ensures that only one instance of the configuration exists."""
+ if cls._instance is None:
+ cls._instance = super(OpenlitConfig, cls).__new__(cls)
+ cls.reset_to_defaults()
+ return cls._instance
+
+ @classmethod
+ def reset_to_defaults(cls):
+ """Resets configuration to default values."""
+ cls.environment = "default"
+ cls.application_name = "default"
+ cls.pricing_info = {}
+ cls.tracer = None
+ cls.event_provider = None
+ cls.metrics_dict = {}
+ cls.otlp_endpoint = None
+ cls.otlp_headers = None
+ cls.disable_batch = False
+ cls.capture_message_content = True
+ cls.disable_metrics = False
+ cls.detailed_tracing = True
+ cls.capture_parameters = False
+ cls.enable_sqlcommenter = False
+ cls.evals_logs_export = True
+ cls.max_content_length = None # None = no truncation
+
+ @classmethod
+ def update_config(
+ cls,
+ environment,
+ application_name,
+ tracer,
+ event_provider,
+ otlp_endpoint,
+ otlp_headers,
+ disable_batch,
+ capture_message_content,
+ metrics_dict,
+ disable_metrics,
+ pricing_info,
+ detailed_tracing,
+ capture_parameters=False,
+ enable_sqlcommenter=False,
+ evals_logs_export=True,
+ max_content_length=None,
+ ):
+ """
+ Updates the configuration based on provided parameters.
+
+ Args:
+ environment (str): Deployment environment.
+ application_name (str): Application name.
+ tracer: Tracer instance.
+ event_provider: Event logger provider instance.
+ otlp_endpoint (str): OTLP endpoint.
+ otlp_headers (Dict[str, str]): OTLP headers.
+ disable_batch (bool): Disable batch span processing flag.
+ capture_message_content (bool): Enable or disable content tracing.
+ metrics_dict: Dictionary of metrics.
+ disable_metrics (bool): Flag to disable metrics.
+ pricing_info (dict): Already-resolved pricing information dict.
+ detailed_tracing (bool): Flag to enable detailed component-level tracing.
+ capture_parameters (bool): Capture database query parameters (security risk).
+ enable_sqlcommenter (bool): Inject trace context as SQL comments.
+ evals_logs_export (bool): Emit evaluation results as OTEL Log Records instead of OTEL Events.
+ max_content_length: Maximum character length for captured content (None = no limit).
+ """
+ cls.environment = environment
+ cls.application_name = application_name
+ cls.pricing_info = pricing_info
+ cls.tracer = tracer
+ cls.event_provider = event_provider
+ cls.metrics_dict = metrics_dict
+ cls.otlp_endpoint = otlp_endpoint
+ cls.otlp_headers = otlp_headers
+ cls.disable_batch = disable_batch
+ cls.capture_message_content = capture_message_content
+ cls.disable_metrics = disable_metrics
+ cls.detailed_tracing = detailed_tracing
+ cls.capture_parameters = capture_parameters
+ cls.enable_sqlcommenter = enable_sqlcommenter
+ cls.evals_logs_export = evals_logs_export
+ cls.max_content_length = max_content_length
diff --git a/sdk/python/src/openlit/cli/config.py b/sdk/python/src/openlit/cli/config.py
index 33314363..c443dd87 100644
--- a/sdk/python/src/openlit/cli/config.py
+++ b/sdk/python/src/openlit/cli/config.py
@@ -116,6 +116,12 @@ PARAMETER_CONFIG = {
"cli_help": "Emit evaluation results as OTEL Log Records instead of OTEL Events",
"cli_type": bool,
},
+ "max_content_length": {
+ "default": None,
+ "env_var": "OPENLIT_MAX_CONTENT_LENGTH",
+ "cli_help": "Maximum character length for captured content (None = no limit)",
+ "cli_type": int,
+ },
# Parameters that are not exposed via CLI (internal use only)
"tracer": {
"default": None,
diff --git a/sdk/python/src/openlit/evals/utils.py b/sdk/python/src/openlit/evals/utils.py
index 6d3050aa..ceaa0915 100644
--- a/sdk/python/src/openlit/evals/utils.py
+++ b/sdk/python/src/openlit/evals/utils.py
@@ -9,6 +9,7 @@ from pydantic import BaseModel
from anthropic import Anthropic
from openai import OpenAI
from openlit.semcov import SemanticConvention
+from openlit._config import OpenlitConfig
# Initialize logger for logging potential issues and operations
logger = logging.getLogger(__name__)
@@ -262,12 +263,8 @@ def get_event_provider():
The event provider if OpenLIT has been initialized with telemetry, else None.
"""
try:
- # pylint: disable=cyclic-import
- # (Import is inside function body, executed only after openlit.init())
- from openlit import OpenlitConfig
-
return OpenlitConfig.event_provider
- except (ImportError, AttributeError):
+ except AttributeError:
return None
@@ -279,11 +276,8 @@ def get_evals_logs_export_config():
True if OTEL Log Records should be used instead of Events, else False.
"""
try:
- # pylint: disable=cyclic-import
- from openlit import OpenlitConfig
-
return getattr(OpenlitConfig, "evals_logs_export", True)
- except (ImportError, AttributeError):
+ except AttributeError:
return True
diff --git a/sdk/python/src/openlit/instrumentation/agno/utils.py b/sdk/python/src/openlit/instrumentation/agno/utils.py
index ddc3e3a1..84ed432f 100644
--- a/sdk/python/src/openlit/instrumentation/agno/utils.py
+++ b/sdk/python/src/openlit/instrumentation/agno/utils.py
@@ -7,6 +7,7 @@ import logging
import time
from openlit.__helpers import (
common_span_attributes,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -126,7 +127,7 @@ def process_agent_request(
if hasattr(instance, "instructions") and instance.instructions:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS,
- str(instance.instructions)[:500],
+ truncate_content(instance.instructions),
)
if hasattr(instance, "agent_id") and instance.agent_id:
@@ -134,32 +135,33 @@ def process_agent_request(
if hasattr(instance, "description") and instance.description:
span.set_attribute(
- SemanticConvention.GEN_AI_AGENT_DESCRIPTION, str(instance.description)[:500]
+ SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
+ truncate_content(instance.description),
)
if hasattr(instance, "introduction") and instance.introduction:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_INTRODUCTION,
- str(instance.introduction)[:500],
+ truncate_content(instance.introduction),
)
# Add request-specific attributes from args and kwargs
if args and args[0]:
if capture_message_content:
span.set_attribute(
- SemanticConvention.GEN_AI_INPUT_MESSAGES, str(args[0])[:1000]
+ SemanticConvention.GEN_AI_INPUT_MESSAGES, truncate_content(args[0])
)
# agno 2.x versions agent.run use input instead of args[0]
if kwargs and kwargs.get("input", None) and capture_message_content:
span.set_attribute(
- SemanticConvention.GEN_AI_INPUT_MESSAGES, str(kwargs["input"])[:1000]
+ SemanticConvention.GEN_AI_INPUT_MESSAGES, truncate_content(kwargs["input"])
)
# agno 2.x versions team.run use input_message instead of args[0]
if kwargs and kwargs.get("input_message", None) and capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
- str(kwargs["input_message"])[:1000],
+ truncate_content(kwargs["input_message"]),
)
# User and session information
@@ -206,10 +208,10 @@ def process_agent_request(
if response:
if capture_message_content:
if hasattr(response, "content") and response.content:
- content = str(response.content)[:2000]
+ content = truncate_content(response.content)
span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES, content)
elif hasattr(response, "message") and response.message:
- content = str(response.message)[:2000]
+ content = truncate_content(response.message)
span.set_attribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES, content)
# Capture additional response metadata
@@ -283,12 +285,13 @@ def process_tool_request(
):
span.set_attribute(
SemanticConvention.GEN_AI_TOOL_DESCRIPTION,
- str(instance.function.description)[:500],
+ truncate_content(instance.function.description),
)
if hasattr(instance, "arguments") and instance.arguments:
span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_PARAMETERS, str(instance.arguments)[:1000]
+ SemanticConvention.GEN_AI_TOOL_PARAMETERS,
+ truncate_content(instance.arguments),
)
# Set execution metrics
@@ -305,11 +308,12 @@ def process_tool_request(
if capture_message_content:
if hasattr(response, "result") and response.result:
span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_OUTPUT, str(response.result)[:1000]
+ SemanticConvention.GEN_AI_TOOL_OUTPUT,
+ truncate_content(response.result),
)
if hasattr(response, "error") and response.error:
span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_ERROR, str(response.error)[:500]
+ SemanticConvention.GEN_AI_TOOL_ERROR, truncate_content(response.error)
)
@@ -372,7 +376,7 @@ def process_memory_request(
# Add input data
if args and capture_message_content:
span.set_attribute(
- "gen_ai.memory.input", str(args[0])[:1000] if args[0] else ""
+ "gen_ai.memory.input", truncate_content(args[0]) if args[0] else ""
)
# Add metadata from kwargs
@@ -381,7 +385,9 @@ def process_memory_request(
if "agent_id" in kwargs:
span.set_attribute("gen_ai.memory.agent_id", kwargs["agent_id"])
if "metadata" in kwargs:
- span.set_attribute("gen_ai.memory.metadata", str(kwargs["metadata"])[:500])
+ span.set_attribute(
+ "gen_ai.memory.metadata", truncate_content(kwargs["metadata"])
+ )
# Set execution metrics
span.set_attribute("gen_ai.memory.operation.duration", execution_time)
@@ -534,7 +540,8 @@ def process_vectordb_request(
if query:
if isinstance(query, str):
span.set_attribute(
- SemanticConvention.GEN_AI_VECTORDB_SEARCH_QUERY, query[:500]
+ SemanticConvention.GEN_AI_VECTORDB_SEARCH_QUERY,
+ truncate_content(query),
)
elif hasattr(query, "__len__"):
span.set_attribute(
@@ -621,7 +628,8 @@ def process_knowledge_request(
query = args[0] if args else None
if query and capture_message_content:
span.set_attribute(
- SemanticConvention.GEN_AI_KNOWLEDGE_SEARCH_QUERY, query[:500]
+ SemanticConvention.GEN_AI_KNOWLEDGE_SEARCH_QUERY,
+ truncate_content(query),
)
limit = kwargs.get("limit", args[1] if len(args) > 1 else 5)
@@ -711,7 +719,7 @@ def process_workflow_request(
if hasattr(instance, "description") and instance.description:
span.set_attribute(
SemanticConvention.GEN_AI_WORKFLOW_DESCRIPTION,
- str(instance.description)[:300],
+ truncate_content(instance.description),
)
# Set execution metrics
diff --git a/sdk/python/src/openlit/instrumentation/ai21/utils.py b/sdk/python/src/openlit/instrumentation/ai21/utils.py
index cef34601..35b686a9 100644
--- a/sdk/python/src/openlit/instrumentation/ai21/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ai21/utils.py
@@ -17,6 +17,7 @@ from openlit.__helpers import (
common_span_attributes,
record_completion_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -102,6 +103,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/anthropic/utils.py b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
index 753ed14b..bbba71e9 100644
--- a/sdk/python/src/openlit/instrumentation/anthropic/utils.py
+++ b/sdk/python/src/openlit/instrumentation/anthropic/utils.py
@@ -17,6 +17,7 @@ from openlit.__helpers import (
common_span_attributes,
general_tokens,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -295,6 +296,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
index f1baed07..11d1f71e 100644
--- a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
@@ -13,6 +13,7 @@ from openlit.__helpers import (
get_audio_model_cost,
otel_event,
record_audio_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -72,6 +73,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/astra/utils.py b/sdk/python/src/openlit/instrumentation/astra/utils.py
index 1bcbdb01..d1c4bb19 100644
--- a/sdk/python/src/openlit/instrumentation/astra/utils.py
+++ b/sdk/python/src/openlit/instrumentation/astra/utils.py
@@ -8,6 +8,7 @@ from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
common_db_span_attributes,
record_db_metrics,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -216,8 +217,8 @@ def common_astra_logic(
scope._span.set_attribute(
SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}... "
- f"update={str(update_query)[:100]}...",
+ f"filter={truncate_content(filter_query)} "
+ f"update={truncate_content(update_query)}",
)
elif scope._db_operation == SemanticConvention.DB_OPERATION_REPLACE:
@@ -240,7 +241,7 @@ def common_astra_logic(
scope._span.set_attribute(
SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}... "
+ f"filter={truncate_content(filter_query)} "
f"upsert={scope._kwargs.get('upsert', False)}",
)
@@ -262,7 +263,7 @@ def common_astra_logic(
scope._span.set_attribute(
SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}...",
+ f"filter={truncate_content(filter_query)}",
)
elif scope._db_operation in [
@@ -287,7 +288,7 @@ def common_astra_logic(
scope._span.set_attribute(
SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"filter={str(filter_query)[:100]}...",
+ f"filter={truncate_content(filter_query)}",
)
scope._span.set_status(Status(StatusCode.OK))
diff --git a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
index 9b515c90..51623cbd 100644
--- a/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
+++ b/sdk/python/src/openlit/instrumentation/azure_ai_inference/utils.py
@@ -19,6 +19,7 @@ from openlit.__helpers import (
record_embedding_metrics,
general_tokens,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -206,6 +207,8 @@ def build_system_instructions_from_messages(messages):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/bedrock/utils.py b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
index 3a2a9a9a..9fbef871 100644
--- a/sdk/python/src/openlit/instrumentation/bedrock/utils.py
+++ b/sdk/python/src/openlit/instrumentation/bedrock/utils.py
@@ -18,6 +18,7 @@ from openlit.__helpers import (
handle_exception,
general_tokens,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -202,6 +203,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/browser_use/async_browser_use.py b/sdk/python/src/openlit/instrumentation/browser_use/async_browser_use.py
index 04c747af..7704a0dc 100644
--- a/sdk/python/src/openlit/instrumentation/browser_use/async_browser_use.py
+++ b/sdk/python/src/openlit/instrumentation/browser_use/async_browser_use.py
@@ -11,7 +11,11 @@ from typing import Any, List
from opentelemetry.trace import SpanKind
from opentelemetry import context as context_api
-from openlit.__helpers import handle_exception, common_framework_span_attributes
+from openlit.__helpers import (
+ handle_exception,
+ common_framework_span_attributes,
+ truncate_content,
+)
from openlit.instrumentation.browser_use.utils import (
BrowserUseInstrumentationContext,
get_operation_name,
@@ -474,19 +478,19 @@ async def _create_detailed_step_span(
if hasattr(model_output, "thinking") and model_output.thinking:
step_span.set_attribute(
SemanticConvention.GEN_AI_AGENT_THINKING,
- str(model_output.thinking)[:500],
+ truncate_content(model_output.thinking),
)
if hasattr(model_output, "memory") and model_output.memory:
step_span.set_attribute(
SemanticConvention.GEN_AI_AGENT_MEMORY,
- str(model_output.memory)[:500],
+ truncate_content(model_output.memory),
)
if hasattr(model_output, "next_goal") and model_output.next_goal:
step_span.set_attribute(
SemanticConvention.GEN_AI_AGENT_NEXT_GOAL,
- str(model_output.next_goal)[:200],
+ truncate_content(model_output.next_goal),
)
if (
@@ -495,7 +499,7 @@ async def _create_detailed_step_span(
):
step_span.set_attribute(
SemanticConvention.GEN_AI_AGENT_EVALUATION,
- str(model_output.evaluation_previous_goal)[:200],
+ truncate_content(model_output.evaluation_previous_goal),
)
# Actions taken in this step
@@ -541,7 +545,8 @@ async def _create_detailed_step_span(
if hasattr(state, "title") and state.title:
step_span.set_attribute(
- SemanticConvention.GEN_AI_AGENT_PAGE_TITLE, state.title[:100]
+ SemanticConvention.GEN_AI_AGENT_PAGE_TITLE,
+ truncate_content(state.title),
)
# Tab information
@@ -574,7 +579,7 @@ async def _create_detailed_step_span(
error_count += 1
if hasattr(result, "error") and result.error:
- error_messages.append(str(result.error)[:100])
+ error_messages.append(truncate_content(result.error))
step_span.set_attribute(
SemanticConvention.GEN_AI_AGENT_ACTIONS_SUCCESS_COUNT, success_count
@@ -662,7 +667,7 @@ async def _create_individual_action_spans(
"num_pages",
]:
action_span.set_attribute(
- f"gen_ai.action.{key}", str(value)[:200]
+ f"gen_ai.action.{key}", truncate_content(value)
)
# Special handling for sensitive data
@@ -682,7 +687,7 @@ async def _create_individual_action_spans(
)
action_span.set_attribute(
SemanticConvention.GEN_AI_ACTION_FILE_PATH,
- str(file_path)[:100],
+ truncate_content(file_path),
)
# Enhanced action result tracking
@@ -700,7 +705,7 @@ async def _create_individual_action_spans(
):
action_span.set_attribute(
SemanticConvention.GEN_AI_ACTION_ERROR,
- str(result.error)[:200],
+ truncate_content(result.error),
)
if (
hasattr(result, "extracted_content")
@@ -722,7 +727,7 @@ async def _create_individual_action_spans(
if hasattr(step.state, "title") and step.state.title:
action_span.set_attribute(
SemanticConvention.GEN_AI_BROWSER_PAGE_TITLE,
- step.state.title[:100],
+ truncate_content(step.state.title),
)
if hasattr(step.state, "tabs") and step.state.tabs:
action_span.set_attribute(
@@ -806,7 +811,7 @@ def _process_enhanced_response(
if final_result and capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_FINAL_RESULT,
- str(final_result)[:1000],
+ truncate_content(final_result),
)
# Usage summary if available
@@ -847,7 +852,7 @@ def _process_enhanced_response(
if capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_ACTION_ERROR,
- str(response.error)[:200],
+ truncate_content(response.error),
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/browser_use/browser_use.py b/sdk/python/src/openlit/instrumentation/browser_use/browser_use.py
index eb4d0fad..d16c94f8 100644
--- a/sdk/python/src/openlit/instrumentation/browser_use/browser_use.py
+++ b/sdk/python/src/openlit/instrumentation/browser_use/browser_use.py
@@ -10,7 +10,11 @@ from typing import Any
from opentelemetry.trace import SpanKind
from opentelemetry import context as context_api
-from openlit.__helpers import handle_exception, common_framework_span_attributes
+from openlit.__helpers import (
+ handle_exception,
+ common_framework_span_attributes,
+ truncate_content,
+)
from openlit.instrumentation.browser_use.utils import (
BrowserUseInstrumentationContext,
get_operation_name,
@@ -353,7 +357,7 @@ def _process_enhanced_response(
if final_result and capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_FINAL_RESULT,
- str(final_result)[:1000],
+ truncate_content(final_result),
)
# Usage summary if available
@@ -394,7 +398,7 @@ def _process_enhanced_response(
if capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_ACTION_ERROR,
- str(response.error)[:200],
+ truncate_content(response.error),
)
except Exception as e:
diff --git a/sdk/python/src/openlit/instrumentation/browser_use/utils.py b/sdk/python/src/openlit/instrumentation/browser_use/utils.py
index b3d6fd89..fb38abbb 100644
--- a/sdk/python/src/openlit/instrumentation/browser_use/utils.py
+++ b/sdk/python/src/openlit/instrumentation/browser_use/utils.py
@@ -5,7 +5,7 @@ Utilities for Browser-Use instrumentation with proper OpenLIT semantic conventio
import json
import logging
from typing import Any, Dict, Optional
-from openlit.__helpers import get_chat_model_cost
+from openlit.__helpers import get_chat_model_cost, truncate_content
from openlit.semcov import SemanticConvention
logger = logging.getLogger(__name__)
@@ -79,18 +79,18 @@ class BrowserUseInstrumentationContext:
if self.args and len(self.args) > 0:
first_arg = self.args[0]
if isinstance(first_arg, str):
- return first_arg[:200] # Limit length
+ return truncate_content(first_arg)
# Check kwargs for task-related parameters
for key in ["task", "instruction", "description"]:
if key in self.kwargs:
value = self.kwargs[key]
if isinstance(value, str):
- return value[:200]
+ return truncate_content(value)
# Check instance attributes
if hasattr(self.instance, "task"):
- return str(self.instance.task)[:200]
+ return truncate_content(self.instance.task)
return "browser_automation_task"
except (AttributeError, ValueError, TypeError):
@@ -369,11 +369,14 @@ def process_response(
if capture_message_content:
if isinstance(response, str):
span.set_attribute(
- SemanticConvention.GEN_AI_OUTPUT_MESSAGES, response[:1000]
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
+ truncate_content(response),
)
elif hasattr(response, "__dict__"):
try:
- content = json.dumps(response.__dict__, default=str)[:1000]
+ content = truncate_content(
+ json.dumps(response.__dict__, default=str)
+ )
span.set_attribute(
SemanticConvention.GEN_AI_OUTPUT_MESSAGES, content
)
@@ -391,18 +394,19 @@ def capture_agent_thoughts_and_state(span: Any, agent_output: Any) -> None:
if hasattr(agent_output, "thinking") and agent_output.thinking:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_THINKING,
- str(agent_output.thinking)[:500],
+ truncate_content(agent_output.thinking),
)
if hasattr(agent_output, "memory") and agent_output.memory:
span.set_attribute(
- SemanticConvention.GEN_AI_AGENT_MEMORY, str(agent_output.memory)[:500]
+ SemanticConvention.GEN_AI_AGENT_MEMORY,
+ truncate_content(agent_output.memory),
)
if hasattr(agent_output, "next_goal") and agent_output.next_goal:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_NEXT_GOAL,
- str(agent_output.next_goal)[:200],
+ truncate_content(agent_output.next_goal),
)
if (
@@ -411,7 +415,7 @@ def capture_agent_thoughts_and_state(span: Any, agent_output: Any) -> None:
):
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_EVALUATION,
- str(agent_output.evaluation_previous_goal)[:200],
+ truncate_content(agent_output.evaluation_previous_goal),
)
except (AttributeError, ValueError, TypeError) as e:
diff --git a/sdk/python/src/openlit/instrumentation/cohere/utils.py b/sdk/python/src/openlit/instrumentation/cohere/utils.py
index efb5bade..4b4226ce 100644
--- a/sdk/python/src/openlit/instrumentation/cohere/utils.py
+++ b/sdk/python/src/openlit/instrumentation/cohere/utils.py
@@ -18,6 +18,7 @@ from openlit.__helpers import (
record_completion_metrics,
record_embedding_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -120,6 +121,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/crawl4ai/utils.py b/sdk/python/src/openlit/instrumentation/crawl4ai/utils.py
index b4eff3f6..072c3479 100644
--- a/sdk/python/src/openlit/instrumentation/crawl4ai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/crawl4ai/utils.py
@@ -13,7 +13,7 @@ from opentelemetry.sdk.resources import (
DEPLOYMENT_ENVIRONMENT,
)
-from openlit.__helpers import handle_exception
+from openlit.__helpers import handle_exception, truncate_content
from openlit.semcov import SemanticConvention
# Initialize logger for logging potential issues and operations
@@ -487,14 +487,11 @@ def process_crawl_response(
def format_content(content, max_length: int = 1000) -> str:
- """Format content for tracing with length limits."""
+ """Format content for tracing, delegating to the global truncation config."""
if content is None:
return ""
- content_str = str(content)
- if len(content_str) > max_length:
- return content_str[:max_length] + "..."
- return content_str
+ return truncate_content(content)
def capture_message_content_if_enabled(
@@ -804,7 +801,9 @@ def capture_extraction_content(
)
elif hasattr(extraction_input, "__iter__"):
# Handle list/tuple of inputs
- combined_input = " ".join(str(item)[:200] for item in extraction_input)
+ combined_input = " ".join(
+ truncate_content(item) for item in extraction_input
+ )
input_content = format_content(combined_input, max_length=1000)
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES, input_content
diff --git a/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py b/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
index 5d7f5bdf..7704b882 100644
--- a/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
+++ b/sdk/python/src/openlit/instrumentation/elevenlabs/utils.py
@@ -13,6 +13,7 @@ from openlit.__helpers import (
get_audio_model_cost,
record_audio_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -53,6 +54,8 @@ def build_output_messages(output_type="speech"):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/firecrawl/utils.py b/sdk/python/src/openlit/instrumentation/firecrawl/utils.py
index 91ddeaf8..138f06ca 100644
--- a/sdk/python/src/openlit/instrumentation/firecrawl/utils.py
+++ b/sdk/python/src/openlit/instrumentation/firecrawl/utils.py
@@ -9,6 +9,7 @@ from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
common_framework_span_attributes,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -497,7 +498,7 @@ def handle_firecrawl_error(span, error):
span.set_attribute("http.status_code", response.status_code)
if hasattr(response, "text"):
span.set_attribute(
- "error.response_text", response.text[:500]
+ "error.response_text", truncate_content(response.text)
) # Limit error text
# Additional error categorization using standard patterns
diff --git a/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
index 6f1babee..a318eeaf 100644
--- a/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
+++ b/sdk/python/src/openlit/instrumentation/google_ai_studio/utils.py
@@ -17,6 +17,7 @@ from openlit.__helpers import (
general_tokens,
otel_event,
record_completion_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -300,6 +301,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/gpt4all/utils.py b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
index 70df031d..eb258f75 100644
--- a/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
+++ b/sdk/python/src/openlit/instrumentation/gpt4all/utils.py
@@ -18,6 +18,7 @@ from openlit.__helpers import (
record_completion_metrics,
record_embedding_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -68,6 +69,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/groq/utils.py b/sdk/python/src/openlit/instrumentation/groq/utils.py
index 5623a525..eabe9ae8 100644
--- a/sdk/python/src/openlit/instrumentation/groq/utils.py
+++ b/sdk/python/src/openlit/instrumentation/groq/utils.py
@@ -17,6 +17,7 @@ from openlit.__helpers import (
common_span_attributes,
record_completion_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -142,6 +143,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/haystack/utils.py b/sdk/python/src/openlit/instrumentation/haystack/utils.py
index 04125fd8..20fb0e38 100644
--- a/sdk/python/src/openlit/instrumentation/haystack/utils.py
+++ b/sdk/python/src/openlit/instrumentation/haystack/utils.py
@@ -6,7 +6,11 @@ import time
import json
from typing import Dict, Any
from opentelemetry.trace import Status, StatusCode
-from openlit.__helpers import common_framework_span_attributes, record_framework_metrics
+from openlit.__helpers import (
+ common_framework_span_attributes,
+ record_framework_metrics,
+ truncate_content,
+)
from openlit.semcov import SemanticConvention
# Optimized operation mapping - minimal and fast
@@ -173,7 +177,7 @@ def extract_pipeline_metadata(instance, args, kwargs) -> Dict[str, Any]:
sanitized_input[key] = value
elif isinstance(value, dict):
sanitized_input[key] = {
- k: str(v)[:100] for k, v in value.items()
+ k: truncate_content(v) for k, v in value.items()
}
else:
sanitized_input[key] = str(type(value)).__name__
@@ -396,7 +400,7 @@ def process_haystack_response(
if replies and capture_message_content:
span.set_attribute(
SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
- str(replies[0])[:500],
+ truncate_content(replies[0]),
)
break
@@ -435,7 +439,9 @@ def process_haystack_response(
)
if args and capture_message_content:
- span.set_attribute(SemanticConvention.GEN_AI_PROMPT, str(args[0])[:500])
+ span.set_attribute(
+ SemanticConvention.GEN_AI_PROMPT, truncate_content(args[0])
+ )
if isinstance(response, dict) and "replies" in response:
replies = response["replies"]
diff --git a/sdk/python/src/openlit/instrumentation/langchain/__init__.py b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
index 06f8ee0d..944ad16f 100644
--- a/sdk/python/src/openlit/instrumentation/langchain/__init__.py
+++ b/sdk/python/src/openlit/instrumentation/langchain/__init__.py
@@ -372,6 +372,7 @@ def _create_callback_handler_class(
record_framework_metrics,
calculate_ttft,
calculate_tbt,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -736,9 +737,9 @@ def _create_callback_handler_class(
if self._capture_message_content and inputs:
try:
- input_str = json.dumps(inputs, default=str)[:5000]
+ input_str = truncate_content(json.dumps(inputs, default=str))
except Exception:
- input_str = str(inputs)[:5000]
+ input_str = truncate_content(inputs)
span.set_attribute(
SemanticConvention.GEN_AI_WORKFLOW_INPUT, input_str
)
@@ -769,9 +770,9 @@ def _create_callback_handler_class(
if self._capture_message_content and outputs:
try:
- output_str = json.dumps(outputs, default=str)[:5000]
+ output_str = truncate_content(json.dumps(outputs, default=str))
except Exception:
- output_str = str(outputs)[:5000]
+ output_str = truncate_content(outputs)
span.set_attribute(
SemanticConvention.GEN_AI_WORKFLOW_OUTPUT, output_str
)
@@ -812,7 +813,7 @@ def _create_callback_handler_class(
span = self.spans[run_id].span
span.set_attribute(
SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,
- str(error)[:1000],
+ truncate_content(error),
)
self._end_span(run_id, str(error))
except Exception as e:
@@ -857,7 +858,7 @@ def _create_callback_handler_class(
self.spans[run_id].prompts = prompts if prompts else []
if self._capture_message_content and prompts:
- prompt_str = "\n".join(prompts)[:5000]
+ prompt_str = truncate_content("\n".join(prompts))
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES, prompt_str
)
@@ -916,7 +917,7 @@ def _create_callback_handler_class(
role = getattr(msg, "type", "unknown")
content = getattr(msg, "content", str(msg))
formatted.append(f"{role}: {content}")
- prompt_str = "\n".join(formatted)[:5000]
+ prompt_str = truncate_content("\n".join(formatted))
# Store prompt for token estimation and raw messages for common_chat_logic
self.spans[run_id].prompt_content = prompt_str
@@ -1172,7 +1173,7 @@ def _create_callback_handler_class(
span = self.spans[run_id].span
span.set_attribute(
SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,
- str(error)[:1000],
+ truncate_content(error),
)
self._end_span(run_id, str(error))
except Exception as e:
@@ -1211,7 +1212,8 @@ def _create_callback_handler_class(
if self._capture_message_content and input_str:
span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_INPUT, str(input_str)[:5000]
+ SemanticConvention.GEN_AI_TOOL_INPUT,
+ truncate_content(input_str),
)
except Exception as e:
@@ -1240,7 +1242,7 @@ def _create_callback_handler_class(
if self._capture_message_content and output:
span.set_attribute(
- SemanticConvention.GEN_AI_TOOL_OUTPUT, str(output)[:5000]
+ SemanticConvention.GEN_AI_TOOL_OUTPUT, truncate_content(output)
)
# Record metrics
@@ -1279,7 +1281,7 @@ def _create_callback_handler_class(
span = self.spans[run_id].span
span.set_attribute(
SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,
- str(error)[:1000],
+ truncate_content(error),
)
self._end_span(run_id, str(error))
except Exception as e:
@@ -1317,7 +1319,8 @@ def _create_callback_handler_class(
if self._capture_message_content and query:
span.set_attribute(
- SemanticConvention.GEN_AI_RETRIEVAL_QUERY, str(query)[:5000]
+ SemanticConvention.GEN_AI_RETRIEVAL_QUERY,
+ truncate_content(query),
)
except Exception as e:
@@ -1352,7 +1355,7 @@ def _create_callback_handler_class(
sample_docs = []
for doc in documents[:3]:
content = getattr(doc, "page_content", str(doc))
- sample_docs.append(content[:500])
+ sample_docs.append(truncate_content(content))
span.set_attribute(
SemanticConvention.GEN_AI_RETRIEVAL_DOCUMENTS,
"; ".join(sample_docs),
@@ -1394,7 +1397,7 @@ def _create_callback_handler_class(
span = self.spans[run_id].span
span.set_attribute(
SemanticConvention.GEN_AI_FRAMEWORK_ERROR_MESSAGE,
- str(error)[:1000],
+ truncate_content(error),
)
self._end_span(run_id, str(error))
except Exception as e:
@@ -1427,16 +1430,16 @@ def _create_callback_handler_class(
log = getattr(action, "log", "")
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_ACTION_TOOL,
- str(tool)[:1000],
+ truncate_content(tool),
)
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_ACTION_TOOL_INPUT,
- str(tool_input)[:5000],
+ truncate_content(tool_input),
)
if log:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_ACTION_LOG,
- str(log)[:5000],
+ truncate_content(log),
)
except Exception as e:
logger.debug("Error in on_agent_action: %s", e)
@@ -1459,12 +1462,12 @@ def _create_callback_handler_class(
log = getattr(finish, "log", "")
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_FINISH_OUTPUT,
- str(output)[:5000],
+ truncate_content(output),
)
if log:
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_FINISH_LOG,
- str(log)[:5000],
+ truncate_content(log),
)
except Exception as e:
logger.debug("Error in on_agent_finish: %s", e)
diff --git a/sdk/python/src/openlit/instrumentation/langchain/utils.py b/sdk/python/src/openlit/instrumentation/langchain/utils.py
index dde958e4..2312fb60 100644
--- a/sdk/python/src/openlit/instrumentation/langchain/utils.py
+++ b/sdk/python/src/openlit/instrumentation/langchain/utils.py
@@ -16,6 +16,7 @@ from openlit.__helpers import (
calculate_tbt,
general_tokens,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -168,6 +169,8 @@ def _set_span_messages_as_array(
) -> None:
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/langgraph/async_langgraph.py b/sdk/python/src/openlit/instrumentation/langgraph/async_langgraph.py
index aeca52da..e4050576 100644
--- a/sdk/python/src/openlit/instrumentation/langgraph/async_langgraph.py
+++ b/sdk/python/src/openlit/instrumentation/langgraph/async_langgraph.py
@@ -6,7 +6,7 @@ import time
import json
from opentelemetry.trace import SpanKind, Status, StatusCode
from opentelemetry import context as context_api
-from openlit.__helpers import handle_exception
+from openlit.__helpers import handle_exception, truncate_content
from openlit.instrumentation.langgraph.utils import (
process_langgraph_response,
OPERATION_MAP,
@@ -216,7 +216,9 @@ async def _create_async_stream_wrapper(
content = get_message_content(msg)
role = get_message_role(msg)
if content:
- span.set_attribute(f"gen_ai.prompt.{i}.content", content[:500])
+ span.set_attribute(
+ f"gen_ai.prompt.{i}.content", truncate_content(content)
+ )
span.set_attribute(f"gen_ai.prompt.{i}.role", role)
# Extract config information
@@ -364,11 +366,11 @@ def _finalize_async_stream_span(
if execution_state["final_response"] and capture_message_content:
span.set_attribute(
SemanticConvention.LANGGRAPH_FINAL_RESPONSE,
- execution_state["final_response"][:500],
+ truncate_content(execution_state["final_response"]),
)
span.set_attribute(
SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
- execution_state["final_response"][:1000],
+ truncate_content(execution_state["final_response"]),
)
span.set_attribute(SemanticConvention.LANGGRAPH_GRAPH_STATUS, "success")
diff --git a/sdk/python/src/openlit/instrumentation/langgraph/langgraph.py b/sdk/python/src/openlit/instrumentation/langgraph/langgraph.py
index e8a5dd35..4e77ef9e 100644
--- a/sdk/python/src/openlit/instrumentation/langgraph/langgraph.py
+++ b/sdk/python/src/openlit/instrumentation/langgraph/langgraph.py
@@ -5,7 +5,7 @@ LangGraph sync wrapper for instrumentation
import time
from opentelemetry.trace import SpanKind
from opentelemetry import context as context_api
-from openlit.__helpers import handle_exception
+from openlit.__helpers import handle_exception, truncate_content
from openlit.instrumentation.langgraph.utils import (
process_langgraph_response,
OPERATION_MAP,
@@ -191,7 +191,7 @@ def _handle_stream(
role = get_message_role(msg)
if content:
span.set_attribute(
- f"gen_ai.prompt.{i}.content", content[:500]
+ f"gen_ai.prompt.{i}.content", truncate_content(content)
)
span.set_attribute(f"gen_ai.prompt.{i}.role", role)
@@ -310,11 +310,11 @@ def _finalize_stream_span(span, execution_state, capture_message_content, start_
if execution_state["final_response"] and capture_message_content:
span.set_attribute(
SemanticConvention.LANGGRAPH_FINAL_RESPONSE,
- execution_state["final_response"][:500],
+ truncate_content(execution_state["final_response"]),
)
span.set_attribute(
SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
- execution_state["final_response"][:1000],
+ truncate_content(execution_state["final_response"]),
)
span.set_attribute(SemanticConvention.LANGGRAPH_GRAPH_STATUS, "success")
diff --git a/sdk/python/src/openlit/instrumentation/langgraph/utils.py b/sdk/python/src/openlit/instrumentation/langgraph/utils.py
index 5e06de19..eef53403 100644
--- a/sdk/python/src/openlit/instrumentation/langgraph/utils.py
+++ b/sdk/python/src/openlit/instrumentation/langgraph/utils.py
@@ -5,7 +5,7 @@ LangGraph utilities for comprehensive telemetry processing and business intellig
import time
import json
from opentelemetry.trace import Status, StatusCode
-from openlit.__helpers import common_framework_span_attributes
+from openlit.__helpers import common_framework_span_attributes, truncate_content
from openlit.semcov import SemanticConvention
@@ -206,7 +206,9 @@ def extract_llm_info_from_result(span, state, result):
content = get_message_content(msg)
role = get_message_role(msg)
if content:
- span.set_attribute(f"gen_ai.prompt.{i}.content", content[:500])
+ span.set_attribute(
+ f"gen_ai.prompt.{i}.content", truncate_content(content)
+ )
span.set_attribute(f"gen_ai.prompt.{i}.role", role)
# Extract from result
@@ -271,7 +273,8 @@ def extract_llm_info_from_result(span, state, result):
content = get_message_content(last_msg)
if content:
span.set_attribute(
- SemanticConvention.GEN_AI_OUTPUT_MESSAGES, content[:1000]
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
+ truncate_content(content),
)
# Extract usage_metadata (alternative location)
@@ -310,7 +313,8 @@ def extract_llm_info_from_result(span, state, result):
else str(tool_call.args)
)
span.set_attribute(
- f"gen_ai.tool_call.{j}.arguments", args_str[:500]
+ f"gen_ai.tool_call.{j}.arguments",
+ truncate_content(args_str),
)
if hasattr(tool_call, "id"):
span.set_attribute(f"gen_ai.tool_call.{j}.id", tool_call.id)
@@ -576,10 +580,12 @@ def _process_invoke_response(span, response, capture_message_content):
content = get_message_content(last_msg)
if content and capture_message_content:
span.set_attribute(
- SemanticConvention.LANGGRAPH_FINAL_RESPONSE, content[:500]
+ SemanticConvention.LANGGRAPH_FINAL_RESPONSE,
+ truncate_content(content),
)
span.set_attribute(
- SemanticConvention.GEN_AI_OUTPUT_MESSAGES, content[:1000]
+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,
+ truncate_content(content),
)
# Try to extract LLM info
diff --git a/sdk/python/src/openlit/instrumentation/letta/utils.py b/sdk/python/src/openlit/instrumentation/letta/utils.py
index 372702f9..8f8c4428 100644
--- a/sdk/python/src/openlit/instrumentation/letta/utils.py
+++ b/sdk/python/src/openlit/instrumentation/letta/utils.py
@@ -9,6 +9,7 @@ from openlit.__helpers import (
common_framework_span_attributes,
handle_exception,
get_chat_model_cost,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -168,10 +169,7 @@ def _set_letta_specific_attributes(span, kwargs, response, operation_type):
SemanticConvention.GEN_AI_AGENT_TYPE, response.agent_type
)
if hasattr(response, "system"):
- # Truncate long system instructions
- instructions = str(response.system)
- if len(instructions) > 2000:
- instructions = instructions[:2000] + "..."
+ instructions = truncate_content(response.system)
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_INSTRUCTIONS, instructions
)
diff --git a/sdk/python/src/openlit/instrumentation/litellm/utils.py b/sdk/python/src/openlit/instrumentation/litellm/utils.py
index 78b14bc6..76796b78 100644
--- a/sdk/python/src/openlit/instrumentation/litellm/utils.py
+++ b/sdk/python/src/openlit/instrumentation/litellm/utils.py
@@ -19,6 +19,7 @@ from openlit.__helpers import (
record_completion_metrics,
record_embedding_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -213,6 +214,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/milvus/utils.py b/sdk/python/src/openlit/instrumentation/milvus/utils.py
index 66c444bd..3f2b0eb8 100644
--- a/sdk/python/src/openlit/instrumentation/milvus/utils.py
+++ b/sdk/python/src/openlit/instrumentation/milvus/utils.py
@@ -8,6 +8,7 @@ from opentelemetry.trace import Status, StatusCode
from openlit.__helpers import (
common_db_span_attributes,
record_db_metrics,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -234,7 +235,7 @@ def common_milvus_logic(
scope._span.set_attribute(
SemanticConvention.DB_QUERY_SUMMARY,
f"{scope._db_operation} {collection_name} "
- f"data={str(data)[:100]}... "
+ f"data={truncate_content(data)} "
f"limit={scope._kwargs.get('limit', 'None')}",
)
diff --git a/sdk/python/src/openlit/instrumentation/mistral/utils.py b/sdk/python/src/openlit/instrumentation/mistral/utils.py
index 0dd3ce30..4501fd62 100644
--- a/sdk/python/src/openlit/instrumentation/mistral/utils.py
+++ b/sdk/python/src/openlit/instrumentation/mistral/utils.py
@@ -18,6 +18,7 @@ from openlit.__helpers import (
otel_event,
record_completion_metrics,
record_embedding_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -158,6 +159,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/ollama/utils.py b/sdk/python/src/openlit/instrumentation/ollama/utils.py
index 79ce563a..c6ae585f 100644
--- a/sdk/python/src/openlit/instrumentation/ollama/utils.py
+++ b/sdk/python/src/openlit/instrumentation/ollama/utils.py
@@ -19,6 +19,7 @@ from openlit.__helpers import (
record_completion_metrics,
record_embedding_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -144,6 +145,8 @@ def build_tool_definitions(tools):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/openai/utils.py b/sdk/python/src/openlit/instrumentation/openai/utils.py
index a916493f..42b04fbb 100644
--- a/sdk/python/src/openlit/instrumentation/openai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/openai/utils.py
@@ -23,6 +23,7 @@ from openlit.__helpers import (
record_image_metrics,
common_span_attributes,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -422,6 +423,8 @@ def build_system_instructions_from_messages(messages):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py b/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
index 957acac2..6bb262f4 100644
--- a/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
+++ b/sdk/python/src/openlit/instrumentation/openai_agents/processor.py
@@ -11,6 +11,7 @@ from openlit.__helpers import (
common_framework_span_attributes,
handle_exception,
get_chat_model_cost,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -315,7 +316,7 @@ class OpenLITTracingProcessor(TracingProcessor):
if hasattr(agent_span, "instructions"):
span.set_attribute(
SemanticConvention.GEN_AI_AGENT_DESCRIPTION,
- str(agent_span.instructions)[:500],
+ truncate_content(agent_span.instructions),
)
if hasattr(agent_span, "model"):
@@ -328,13 +329,14 @@ class OpenLITTracingProcessor(TracingProcessor):
# Set generation-specific attributes
if hasattr(generation_span, "prompt"):
span.set_attribute(
- SemanticConvention.GEN_AI_PROMPT, str(generation_span.prompt)[:1000]
+ SemanticConvention.GEN_AI_PROMPT,
+ truncate_content(generation_span.prompt),
)
if hasattr(generation_span, "completion"):
span.set_attribute(
SemanticConvention.GEN_AI_COMPLETION,
- str(generation_span.completion)[:1000],
+ truncate_content(generation_span.completion),
)
if hasattr(generation_span, "usage"):
@@ -358,11 +360,13 @@ class OpenLITTracingProcessor(TracingProcessor):
if hasattr(function_span, "arguments"):
span.set_attribute(
- "gen_ai.tool.arguments", str(function_span.arguments)[:500]
+ "gen_ai.tool.arguments", truncate_content(function_span.arguments)
)
if hasattr(function_span, "result"):
- span.set_attribute("gen_ai.tool.result", str(function_span.result)[:500])
+ span.set_attribute(
+ "gen_ai.tool.result", truncate_content(function_span.result)
+ )
def _process_handoff_span(self, span, handoff_span):
"""Process handoff span data."""
@@ -370,7 +374,9 @@ class OpenLITTracingProcessor(TracingProcessor):
span.set_attribute("gen_ai.handoff.target_agent", handoff_span.target_agent)
if hasattr(handoff_span, "reason"):
- span.set_attribute("gen_ai.handoff.reason", str(handoff_span.reason)[:200])
+ span.set_attribute(
+ "gen_ai.handoff.reason", truncate_content(handoff_span.reason)
+ )
def span_end(self, span_data, trace_id: str):
"""Handle span end events."""
diff --git a/sdk/python/src/openlit/instrumentation/premai/utils.py b/sdk/python/src/openlit/instrumentation/premai/utils.py
index 9e618a31..111a99e4 100644
--- a/sdk/python/src/openlit/instrumentation/premai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/premai/utils.py
@@ -19,6 +19,7 @@ from openlit.__helpers import (
otel_event,
record_completion_metrics,
record_embedding_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -159,6 +160,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/psycopg/utils.py b/sdk/python/src/openlit/instrumentation/psycopg/utils.py
index f3cdfdb8..de8bbaa0 100644
--- a/sdk/python/src/openlit/instrumentation/psycopg/utils.py
+++ b/sdk/python/src/openlit/instrumentation/psycopg/utils.py
@@ -9,6 +9,7 @@ from opentelemetry.trace import Status, StatusCode, get_current_span
from openlit.__helpers import (
common_db_span_attributes,
record_db_metrics,
+ truncate_content,
)
from openlit.semcov import SemanticConvention
@@ -478,10 +479,7 @@ def common_psycopg_logic(
# Set query text if capture is enabled
if capture_message_content and scope._query:
- query_str = str(scope._query)
- # Truncate very long queries
- if len(query_str) > 4096:
- query_str = query_str[:4096] + "..."
+ query_str = truncate_content(scope._query)
scope._span.set_attribute(SemanticConvention.DB_QUERY_TEXT, query_str)
# Set query parameters if capture is enabled
diff --git a/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py b/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
index 45620b3c..21aa154b 100644
--- a/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/pydantic_ai/utils.py
@@ -13,7 +13,7 @@ from opentelemetry.sdk.resources import (
)
from opentelemetry.trace import Status, StatusCode, SpanKind
from opentelemetry import context as context_api
-from openlit.__helpers import handle_exception
+from openlit.__helpers import handle_exception, truncate_content
from openlit.semcov import SemanticConvention
# Try to import enhanced helpers for business intelligence
@@ -366,7 +366,7 @@ def extract_context_info(args, kwargs) -> Dict[str, Any]:
# Extract user input
if hasattr(context, "user_input"):
- info["user_input"] = str(context.user_input)[:50]
+ info["user_input"] = truncate_content(context.user_input)
# Extract tool information
if hasattr(context, "tool_calls") and context.tool_calls:
diff --git a/sdk/python/src/openlit/instrumentation/reka/utils.py b/sdk/python/src/openlit/instrumentation/reka/utils.py
index 60d31590..df9a7b8e 100644
--- a/sdk/python/src/openlit/instrumentation/reka/utils.py
+++ b/sdk/python/src/openlit/instrumentation/reka/utils.py
@@ -16,6 +16,7 @@ from openlit.__helpers import (
common_span_attributes,
record_completion_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -148,6 +149,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/sarvam/utils.py b/sdk/python/src/openlit/instrumentation/sarvam/utils.py
index 73638ad7..09428b6a 100644
--- a/sdk/python/src/openlit/instrumentation/sarvam/utils.py
+++ b/sdk/python/src/openlit/instrumentation/sarvam/utils.py
@@ -19,6 +19,7 @@ from openlit.__helpers import (
otel_event,
get_audio_model_cost,
record_audio_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -96,6 +97,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/transformers/utils.py b/sdk/python/src/openlit/instrumentation/transformers/utils.py
index 5f9762c1..4ed97e72 100644
--- a/sdk/python/src/openlit/instrumentation/transformers/utils.py
+++ b/sdk/python/src/openlit/instrumentation/transformers/utils.py
@@ -14,6 +14,7 @@ from openlit.__helpers import (
common_span_attributes,
otel_event,
record_completion_metrics,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -138,6 +139,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/vertexai/utils.py b/sdk/python/src/openlit/instrumentation/vertexai/utils.py
index 15463446..0394b1a5 100644
--- a/sdk/python/src/openlit/instrumentation/vertexai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/vertexai/utils.py
@@ -16,6 +16,7 @@ from openlit.__helpers import (
record_completion_metrics,
common_span_attributes,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -140,6 +141,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,
diff --git a/sdk/python/src/openlit/instrumentation/vllm/utils.py b/sdk/python/src/openlit/instrumentation/vllm/utils.py
index 4cfef1a5..a2b13781 100644
--- a/sdk/python/src/openlit/instrumentation/vllm/utils.py
+++ b/sdk/python/src/openlit/instrumentation/vllm/utils.py
@@ -14,6 +14,7 @@ from openlit.__helpers import (
common_span_attributes,
record_completion_metrics,
otel_event,
+ truncate_message_content,
)
from openlit.semcov import SemanticConvention
@@ -82,6 +83,8 @@ def build_output_messages(response_text, finish_reason, tool_calls=None):
def _set_span_messages_as_array(span, input_messages, output_messages):
"""Set gen_ai.input.messages and gen_ai.output.messages on span as JSON array strings (OTel)."""
try:
+ truncate_message_content(input_messages)
+ truncate_message_content(output_messages)
if input_messages is not None:
span.set_attribute(
SemanticConvention.GEN_AI_INPUT_MESSAGES,