Tracing
Overview
Create and manage traces, spans, observations, generations, and events.
The Interactive client wraps OpenTelemetry spans with InteractiveAI-specific metadata. Use start_as_current_span / start_as_current_observation for context-manager-based tracing and start_span / start_observation when you need manual span.end() control.
start_span (source)
Create a new span for tracing a unit of work.
This method creates a new span but does not set it as the current span in the context. To create and use a span within a context, use start_as_current_span().
The created span will be the child of the current span in the context.
start_span(
*,
trace_context: TraceContext | None = None,
name: str,
input: Any | None = None,
output: Any | None = None,
metadata: Any | None = None,
version: str | None = None,
level: Literal['DEBUG', 'DEFAULT', 'WARNING', 'ERROR'] | None = None,
status_message: str | None = None,
) -> InteractiveAISpanParameters
trace_context— Optional context for connecting to an existing tracename— Name of the span (e.g., function or operation name)input— Input data for the operation (can be any JSON-serializable object)output— Output data from the operation (can be any JSON-serializable object)metadata— Additional metadata to associate with the spanversion— Version identifier for the code or componentlevel— Importance level of the span (info, warning, error)status_message— Optional status message for the span
Returns
A InteractiveAISpan object that must be ended with .end() when the operation completes
Example
start_as_current_span (source)
Create a new span and set it as the current span in a context manager.
This method creates a new span and sets it as the current span within a context manager. Use this method with a 'with' statement to automatically handle span lifecycle within a code block.
The created span will be the child of the current span in the context.
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the span (e.g., function or operation name)input— Input data for the operation (can be any JSON-serializable object)output— Output data from the operation (can be any JSON-serializable object)metadata— Additional metadata to associate with the spanversion— Version identifier for the code or componentlevel— Importance level of the span (info, warning, error)status_message— Optional status message for the spanend_on_exit— Whether to end the span automatically when leaving the context manager. If False, the span must be manually ended to avoid memory leaks.
Returns
A context manager that yields a InteractiveAISpan
Example
start_observation (source)
Create a new observation of the specified type.
This method creates a new observation but does not set it as the current span in the context. To create and use an observation within a context, use start_as_current_observation().
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the observationas_type— Type of observation to create (defaults to "span")input— Input data for the operationoutput— Output data from the operationmetadata— Additional metadata to associate with the observationversion— Version identifier for the code or componentlevel— Importance level of the observationstatus_message— Optional status message for the observationcompletion_start_time— When the model started generating (for generation types)model— Name/identifier of the AI model used (for generation types)model_parameters— Parameters used for the model (for generation types)usage_details— Token usage information (for generation types)cost_details— Cost information (for generation types)prompt— Associated prompt template (for generation types)
Returns
An observation object of the appropriate type that must be ended with .end()
start_as_current_observation (source)
Create a new observation and set it as the current span in a context manager.
This method creates a new observation of the specified type and sets it as the current span within a context manager. Use this method with a 'with' statement to automatically handle the observation lifecycle within a code block.
The created observation will be the child of the current span in the context.
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the observation (e.g., function or operation name)as_type— Type of observation to create (defaults to "span")input— Input data for the operation (can be any JSON-serializable object)output— Output data from the operation (can be any JSON-serializable object)metadata— Additional metadata to associate with the observationversion— Version identifier for the code or componentlevel— Importance level of the observation (info, warning, error)status_message— Optional status message for the observationend_on_exit— Whether to end the span automatically when leaving the context manager. If False, the span must be manually ended to avoid memory leaks.completion_start_time— When the model started generating the responsemodel— Name/identifier of the AI model used (e.g., "gpt-4")model_parameters— Parameters used for the model (e.g., temperature, max_tokens)usage_details— Token usage information (e.g., prompt_tokens, completion_tokens)cost_details— Cost information for the model callprompt— Associated prompt template from InteractiveAI prompt management
Returns
A context manager that yields the appropriate observation type based on as_type
Example
start_generation (source)
Deprecated: This method is deprecated and will be removed in a future version. Use start_observation(as_type='generation') instead.
Create a new generation span for model generations.
This method creates a specialized span for tracking model generations. It includes additional fields specific to model generations such as model name, token usage, and cost details.
The created generation span will be the child of the current span in the context.
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the generation operationinput— Input data for the model (e.g., prompts)output— Output from the model (e.g., completions)metadata— Additional metadata to associate with the generationversion— Version identifier for the model or componentlevel— Importance level of the generation (info, warning, error)status_message— Optional status message for the generationcompletion_start_time— When the model started generating the responsemodel— Name/identifier of the AI model used (e.g., "gpt-4")model_parameters— Parameters used for the model (e.g., temperature, max_tokens)usage_details— Token usage information (e.g., prompt_tokens, completion_tokens)cost_details— Cost information for the model callprompt— Associated prompt template from InteractiveAI prompt management
Returns
A InteractiveAIGeneration object that must be ended with .end() when complete
Example
start_as_current_generation (source)
Deprecated: This method is deprecated and will be removed in a future version. Use start_as_current_observation(as_type='generation') instead.
Create a new generation span and set it as the current span in a context manager.
This method creates a specialized span for model generations and sets it as the current span within a context manager. Use this method with a 'with' statement to automatically handle the generation span lifecycle within a code block.
The created generation span will be the child of the current span in the context.
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the generation operationinput— Input data for the model (e.g., prompts)output— Output from the model (e.g., completions)metadata— Additional metadata to associate with the generationversion— Version identifier for the model or componentlevel— Importance level of the generation (info, warning, error)status_message— Optional status message for the generationcompletion_start_time— When the model started generating the responsemodel— Name/identifier of the AI model used (e.g., "gpt-4")model_parameters— Parameters used for the model (e.g., temperature, max_tokens)usage_details— Token usage information (e.g., prompt_tokens, completion_tokens)cost_details— Cost information for the model callprompt— Associated prompt template from InteractiveAI prompt managementend_on_exit— Whether to end the span automatically when leaving the context manager. If False, the span must be manually ended to avoid memory leaks.
Returns
A context manager that yields a InteractiveAIGeneration
Example
update_current_generation (source)
Update the current active generation span with new information.
This method updates the current generation span in the active context with additional information. It's useful for adding output, usage stats, or other details that become available during or after model generation.
Parameters
name— The generation nameinput— Updated input data for the modeloutput— Output from the model (e.g., completions)metadata— Additional metadata to associate with the generationversion— Version identifier for the model or componentlevel— Importance level of the generation (info, warning, error)status_message— Optional status message for the generationcompletion_start_time— When the model started generating the responsemodel— Name/identifier of the AI model used (e.g., "gpt-4")model_parameters— Parameters used for the model (e.g., temperature, max_tokens)usage_details— Token usage information (e.g., prompt_tokens, completion_tokens)cost_details— Cost information for the model callprompt— Associated prompt template from InteractiveAI prompt management
Example
update_current_span (source)
Update the current active span with new information.
This method updates the current span in the active context with additional information. It's useful for adding outputs or metadata that become available during execution.
Parameters
name— The span nameinput— Updated input data for the operationoutput— Output data from the operationmetadata— Additional metadata to associate with the spanversion— Version identifier for the code or componentlevel— Importance level of the span (info, warning, error)status_message— Optional status message for the span
Example
update_current_trace (source)
Update the current trace with additional information.
Parameters
name— Updated name for the InteractiveAI traceuser_id— ID of the user who initiated the InteractiveAI tracesession_id— Session identifier for grouping related InteractiveAI tracesversion— Version identifier for the application or serviceinput— Input data for the overall InteractiveAI traceoutput— Output data from the overall InteractiveAI tracemetadata— Additional metadata to associate with the InteractiveAI tracetags— List of tags to categorize the InteractiveAI tracepublic— Whether the InteractiveAI trace should be publicly accessible
create_event (source)
Create a new InteractiveAI observation of type 'EVENT'.
The created InteractiveAI Event observation will be the child of the current span in the context.
Parameters
trace_context— Optional context for connecting to an existing tracename— Name of the span (e.g., function or operation name)input— Input data for the operation (can be any JSON-serializable object)output— Output data from the operation (can be any JSON-serializable object)metadata— Additional metadata to associate with the spanversion— Version identifier for the code or componentlevel— Importance level of the span (info, warning, error)status_message— Optional status message for the span
Returns
The InteractiveAI Event object
Example
create_trace_id (source)
Create a unique trace ID for use with InteractiveAI.
This method generates a unique trace ID for use with various InteractiveAI APIs. It can either generate a random ID or create a deterministic ID based on a seed string.
Trace IDs must be 32 lowercase hexadecimal characters, representing 16 bytes. This method ensures the generated ID meets this requirement. If you need to correlate an external ID with a InteractiveAI trace ID, use the external ID as the seed to get a valid, deterministic InteractiveAI trace ID.
Parameters
seed— Optional string to use as a seed for deterministic ID generation. If provided, the same seed will always produce the same ID. If not provided, a random ID will be generated.
Returns
A 32-character lowercase hexadecimal string representing the InteractiveAI trace ID.
Example
get_current_trace_id (source)
Get the trace ID of the current active span.
This method retrieves the trace ID from the currently active span in the context. It can be used to get the trace ID for referencing in logs, external systems, or for creating related operations.
Returns
The current trace ID as a 32-character lowercase hexadecimal string, or None if there is no active span.
Example
get_current_observation_id (source)
Get the observation ID (span ID) of the current active span.
This method retrieves the observation ID from the currently active span in the context. It can be used to get the observation ID for referencing in logs, external systems, or for creating scores or other related operations.
Returns
The current observation ID as a 16-character lowercase hexadecimal string, or None if there is no active span.
Example
get_trace_url (source)
Get the URL to view a trace in the InteractiveAI UI.
This method generates a URL that links directly to a trace in the InteractiveAI UI. It's useful for providing links in logs, notifications, or debugging tools.
Parameters
trace_id— Optional trace ID to generate a URL for. If not provided, the trace ID of the current active span will be used.
Returns
A URL string pointing to the trace in the InteractiveAI UI, or None if the project ID couldn't be retrieved or no trace ID is available.
Example
Last updated
Was this helpful?

