Span

class opik.api_objects.span.Span(id: str, trace_id: str, project_name: str, message_streamer: Streamer, parent_span_id: str | None = None)

Bases: object

__init__(id: str, trace_id: str, project_name: str, message_streamer: Streamer, parent_span_id: str | None = None)

A Span object. This object should not be created directly, instead use the span method of a Trace (opik.Opik.span()) or another Span (opik.Span.span()).

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[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) None

End the span 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 span. If not provided, the current time will be used.

  • 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.

  • 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:

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[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) None

Update the span attributes.

Parameters:
  • 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.

  • 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:

None

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 = LLMProvider.OPENAI, error_info: ErrorInfoDict | None = None, total_cost: float | None = None) Span

Create a new child span within the current span.

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

  • 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 child span object.

Return type:

Span

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

Log a feedback score for the span.

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

get_distributed_trace_headers() DistributedTraceHeadersDict

Returns headers dictionary to be passed into tracked function on remote node.