Skip to main content

Documentation Index

Fetch the complete documentation index at: https://raindrop.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

Installation

pip install raindrop-dspy dspy

Quick Start

import dspy
from raindrop_dspy import RaindropDSPy

raindrop = RaindropDSPy(api_key="rk_...", user_id="user-123")

lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

predict = dspy.Predict("question -> answer")
wrapped = raindrop.wrap(predict)

result = wrapped(question="What is the capital of France?")
print(result.answer)

raindrop.shutdown()

What Gets Traced

The DSPy integration automatically captures:
  • Module calls — input kwargs, output text (from Prediction result), configured model name
  • Token usage — prompt and completion tokens from get_lm_usage() or the usage attribute
  • Finish reason — completion finish reason (e.g. stop, length) extracted from the LM history
  • Errors — exception type and message captured in event properties, then re-raised
Works with dspy.Predict, dspy.ChainOfThought, and custom dspy.Module subclasses.

Configuration

raindrop = RaindropDSPy(
    api_key="rk_...",                  # Optional: your Raindrop API key (omit to disable telemetry)
    user_id="user-123",                # Optional: associate events with a user
    convo_id="convo-456",              # Optional: conversation/thread ID
    tracing_enabled=True,              # Optional: enable OTEL tracing (default: True)
    bypass_otel_for_tools=True,        # Optional: bypass OTEL for tool calls (default: True)
    debug=True,                        # Optional: enable debug logging (default: False)
)
ParameterTypeDefaultDescription
api_keystr | NoneNoneRaindrop API key. If omitted, telemetry is disabled.
user_idstr | NoneNoneAssociate all events with a user.
convo_idstr | NoneNoneGroup events into a conversation.
tracing_enabledboolTrueEnable OpenTelemetry tracing.
bypass_otel_for_toolsboolTrueBypass OTEL instrumentation for tool calls.
debugboolFalseEnable debug logging (sets logger to DEBUG level).

Identifying Users

Use identify() to associate a user with traits after initialization:
raindrop.identify("user-123", traits={"name": "Alice", "plan": "pro"})
The traits parameter accepts a dictionary with string, int, bool, or float values.

Tracking Signals

Use track_signal() to attach user feedback or edits to an AI event:
# Basic signal
raindrop.track_signal(
    event_id="evt-abc",
    name="thumbs_up",
)

# Feedback with sentiment
raindrop.track_signal(
    event_id="evt-abc",
    name="user_rating",
    signal_type="feedback",
    sentiment="POSITIVE",
    comment="Great response!",
)

# Edit signal
raindrop.track_signal(
    event_id="evt-abc",
    name="user_edit",
    signal_type="edit",
    after="The corrected response text",
)
ParameterTypeDefaultDescription
event_idstrrequiredThe event ID to associate the signal with.
namestrrequiredSignal name (e.g. "thumbs_up", "user_feedback").
signal_type"default" | "feedback" | "edit""default"Signal type.
timestampstr | NoneNoneOptional ISO-8601 timestamp.
propertiesdict | NoneNoneOptional extra properties.
attachment_idstr | NoneNoneOptional attachment identifier.
commentstr | NoneNoneOptional comment text.
afterstr | NoneNoneOptional “after” value for edit signals.
sentiment"POSITIVE" | "NEGATIVE" | NoneNoneOptional sentiment.

Flushing and Shutdown

Always call shutdown() before your process exits to ensure all telemetry is shipped:
raindrop.flush()     # flush pending data
raindrop.shutdown()  # flush + release resources

Finish Reason Tracking

The integration extracts finish_reason from the DSPy LM history automatically. After each call, it inspects dspy.settings.lm.history for the last response and reads response.choices[0].finish_reason. The value (e.g. stop, length) is included in event properties as dspy.finish_reason.

Token Tracking

Token usage is extracted from the DSPy Prediction result:
  1. get_lm_usage() — returns {model: {prompt_tokens, completion_tokens}}. Tokens are summed across models if multiple LMs are used.
  2. usage attribute — fallback for older DSPy versions. Supports both dict and object formats.
Tokens appear in event properties as ai.usage.prompt_tokens and ai.usage.completion_tokens.

Factory Function

The create_raindrop_dspy factory function is available for backwards compatibility:
from raindrop_dspy import create_raindrop_dspy

raindrop = create_raindrop_dspy(
    api_key="rk_...",
    user_id="user-123",
    tracing_enabled=True,
    bypass_otel_for_tools=True,
)

predict = dspy.Predict("question -> answer")
wrapped = raindrop.wrap(predict)

Async Support

DSPy modules with async forward methods are automatically detected and wrapped:
class AsyncPredict(dspy.Module):
    async def forward(self, question: str) -> dspy.Prediction:
        ...

wrapped = raindrop.wrap(AsyncPredict())
result = await wrapped(question="Hello")

Captured Properties

Each event includes the following properties when available:
PropertyDescription
ai.usage.prompt_tokensInput token count
ai.usage.completion_tokensOutput token count
dspy.finish_reasonCompletion finish reason (e.g. stop, length)
error.typeException class name (on error)
error.messageException message (on error)