OpikCallback

class opik.integrations.dspy.OpikCallback(project_name: str | None = None, log_graph: bool = False)

Bases: BaseCallback

Callback for DSPy Opik logging.

Parameters:
  • project_name – The name of the Opik project to log data.

  • log_graph – If True, will log a mermaid diagram for each module

on_module_start(call_id: str, instance: Any, inputs: Dict[str, Any]) None

A handler triggered when forward() method of a module (subclass of dspy.Module) is called.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • instance – The Module instance.

  • inputs – The inputs to the module’s forward() method. Each arguments is stored as a key-value pair in a dictionary.

on_module_end(call_id: str, outputs: Any | None, exception: Exception | None = None) None

A handler triggered after forward() method of a module (subclass of dspy.Module) is executed.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • outputs – The outputs of the module’s forward() method. If the method is interrupted by an exception, this will be None.

  • exception – If an exception is raised during the execution, it will be stored here.

on_lm_start(call_id: str, instance: Any, inputs: Dict[str, Any]) None

A handler triggered when __call__ method of dspy.LM instance is called.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • instance – The LM instance.

  • inputs – The inputs to the LM’s __call__ method. Each arguments is stored as a key-value pair in a dictionary.

on_lm_end(call_id: str, outputs: Dict[str, Any] | None, exception: Exception | None = None) None

A handler triggered after __call__ method of dspy.LM instance is executed.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • outputs – The outputs of the LM’s __call__ method. If the method is interrupted by an exception, this will be None.

  • exception – If an exception is raised during the execution, it will be stored here.

on_tool_start(call_id: str, instance: Any, inputs: Dict[str, Any]) None

A handler triggered when a tool is called.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • instance – The Tool instance.

  • inputs – The inputs to the Tool’s __call__ method. Each arguments is stored as a key-value pair in a dictionary.

on_tool_end(call_id: str, outputs: Dict[str, Any] | None, exception: Exception | None = None) None

A handler triggered after a tool is executed.

Parameters:
  • call_id – A unique identifier for the call. Can be used to connect start/end handlers.

  • outputs – The outputs of the Tool’s __call__ method. If the method is interrupted by an exception, this will be None.

  • exception – If an exception is raised during the execution, it will be stored here.

flush() None

Sends pending Opik data to the backend