Trace

class opik.api_objects.trace.Trace(id: str, message_streamer: Streamer, project_name: str)

Bases: object

__init__(id: str, message_streamer: Streamer, project_name: str)

A Trace object. This object should not be created directly, instead use opik.Opik.trace() to create a new trace.

end(end_time: datetime | None = None, metadata: Dict[str, Any] | None = None, input: Dict[str, Any] | None = None, output: Dict[str, Any] | None = None, tags: List[Any] | None = None, error_info: ErrorInfoDict | None = None, thread_id: str | None = None) None

End the trace and update its attributes.

This method is similar to the update method, but it automatically computes the end time if not provided.

Parameters:
  • end_time – The end time of the trace. If not provided, the current time will be used.

  • metadata – Additional metadata to be associated with the trace.

  • input – The input data for the trace.

  • output – The output data for the trace.

  • tags – A list of tags to be associated with the trace.

  • error_info – The dictionary with error information (typically used when the trace function has failed).

  • thread_id – Used to group multiple traces into a thread. The identifier is user-defined and has to be unique per project.

Returns:

None

update(end_time: datetime | None = None, metadata: Dict[str, Any] | None = None, input: Dict[str, Any] | None = None, output: Dict[str, Any] | None = None, tags: List[Any] | None = None, error_info: ErrorInfoDict | None = None, thread_id: str | None = None) None

Update the trace attributes.

Parameters:
  • end_time – The end time of the trace.

  • metadata – Additional metadata to be associated with the trace.

  • input – The input data for the trace.

  • output – The output data for the trace.

  • tags – A list of tags to be associated with the trace.

  • error_info – The dictionary with error information (typically used when the trace function has failed).

  • thread_id – Used to group multiple traces into a thread. The identifier is user-defined and has to be unique per project.

Returns:

None

span(id: str | None = None, parent_span_id: str | None = None, name: str | None = None, type: Literal['general', 'tool', 'llm'] = 'general', start_time: datetime | None = None, end_time: datetime | None = None, metadata: Dict[str, Any] | None = None, input: Dict[str, Any] | None = None, output: Dict[str, Any] | None = None, tags: List[str] | None = None, usage: Dict[str, Any] | OpikUsage | None = None, model: str | None = None, provider: LLMProvider | str | None = None, error_info: ErrorInfoDict | None = None, total_cost: float | None = None) Span

Create a new span within the trace.

Parameters:
  • id – The ID of the span, should be in UUIDv7 format. If not provided, a new ID will be generated.

  • parent_span_id – The ID of the parent span, if any.

  • name – The name of the span.

  • type – The type of the span. Defaults to “general”.

  • start_time – The start time of the span. If not provided, current time will be used.

  • end_time – The end time of the span.

  • metadata – Additional metadata to be associated with the span.

  • input – The input data for the span.

  • output – The output data for the span.

  • tags – A list of tags to be associated with the span.

  • usage – Usage data for the span. In order for input, output and total tokens to be visible in the UI, the usage must contain OpenAI-formatted keys (they can be passed additionaly to original usage on the top level of the dict): prompt_tokens, completion_tokens and total_tokens. If OpenAI-formatted keys were not found, Opik will try to calculate them automatically if the usage format is recognized (you can see which provider’s formats are recognized in opik.LLMProvider enum), but it is not guaranteed.

  • model – The name of LLM (in this case type parameter should be == llm)

  • provider – The provider of LLM. You can find providers officially supported by Opik for cost tracking in opik.LLMProvider enum. If your provider is not here, please open an issue in our github - https://github.com/comet-ml/opik. If your provider not in the list, you can still specify it but the cost tracking will not be available

  • error_info – The dictionary with error information (typically used when the span function has failed).

  • total_cost – The cost of the span in USD. This value takes priority over the cost calculated by Opik from the usage.

Returns:

The created span object.

Return type:

span.Span

log_feedback_score(name: str, value: float, category_name: str | None = None, reason: str | None = None) None

Log a feedback score for the trace.

Parameters:
  • name – The name of the feedback score.

  • value – The value of the feedback score.

  • category_name – The category name for the feedback score.

  • reason – The reason for the feedback score.

Returns:

None