POST
/
spans
/
{span_id}
/
save_examples
from datetime import datetime, timezone
from opperai import Opper
import time

opper = Opper(http_bearer="YOUR_API_KEY")

# First, create a function to get a dataset_id and then create a span with generation data
created_function = opper.functions.create(
    name=f"content_generator_{int(time.time())}",
    description="Generates high-quality content based on prompts",
    instructions="Generate clear, engaging content based on the provided prompt. Use proper structure and include relevant examples.",
    model="openai/gpt-4o-mini",
)

print(f"Created function with ID: {created_function.id}")

# Create a span with generation data that can be saved to dataset
created_span = opper.spans.create(
    name="content_generation_task",
    start_time=datetime.now(timezone.utc),
    type="generation",
    input="Write a brief introduction to renewable energy",
    output="Renewable energy represents one of the most promising solutions to our growing environmental challenges. Unlike fossil fuels, renewable energy sources such as solar, wind, and hydroelectric power generate electricity without producing harmful emissions. These technologies not only help reduce our carbon footprint but also provide sustainable, long-term energy security for communities worldwide.",
    meta={
        "model": "gpt-4o-mini",
        "function_id": created_function.id,
        "quality_score": 8.5,
    },
)

print(f"Created span with ID: {created_span.id}")

# Save successful generation spans to dataset for training
try:
    dataset_entry = opper.spans.save_to_dataset(span_id=created_span.id)

    print("āœ… Successfully saved span to dataset!")
    print(f"Dataset ID: {dataset_entry.dataset_id}")
    print(f"Dataset Entry ID: {dataset_entry.dataset_entry_id}")
    print(f"Input: {dataset_entry.input[:100]}...")
    print(f"Output: {dataset_entry.output[:100]}...")

    if dataset_entry.expected:
        print(f"Expected: {dataset_entry.expected[:100]}...")

    if dataset_entry.comment:
        print(f"Comment: {dataset_entry.comment}")

    print("\nšŸ“Š This data can now be used for:")
    print("- Fine-tuning models")
    print("- Evaluation benchmarks")
    print("- Few-shot examples")
    print("- Quality analysis")

except Exception as e:
    print(f"āŒ Failed to save to dataset: {e}")
    print("Possible reasons:")
    print("- Span contains no generation data")
    print("- Span is not completed")
    print("- No associated function found")
{
  "dataset_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "dataset_entry_id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
  "input": "<string>",
  "output": "<string>",
  "expected": "<string>",
  "comment": "<string>"
}

Authorizations

Authorization
string
header
required

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

Path Parameters

span_id
string
required

Response

201
application/json

Successful Response

The response is of type object.