Skip to main content

Python SDK for the Langbase API

Project description

Langbase Python SDK

PyPI version Python 3.7+ License: MIT

The official Python SDK for Langbase - Build declarative and composable AI-powered LLM products with ease.

Documentation

Check the Langbase SDK documentation for more details.

The following examples are for reference only. Prefer docs for the latest information.

Features

  • Simple and intuitive API - Get started in minutes
  • Streaming support - Real-time text generation with typed events
  • Type safety - Full type hints for better IDE support
  • Minimal dependencies - Only what you need
  • Python 3.7+ - Support for modern Python versions

Installation

Install Langbase SDK:

pip install langbase

Install dotenv:

pip install dotenv

Quick Start

1. Set up your API key

Create a .env file and add your Langbase API Key.

LANGBASE_API_KEY="your-api-key"
LLM_API_KEY="your-llm-api-key"

2. Initialize the client

from langbase import Langbase
import os
from dotenv import load_dotenv

load_dotenv()

# Get API key from environment variable
langbase_api_key = os.getenv("LANGBASE_API_KEY")
llm_api_key = os.getenv("LLM_API_KEY")

# Initialize the client
langbase = Langbase(api_key=langbase_api_key)
langbase = Langbase(api_key=langbase_api_key)

3. Generate text

# Simple generation
response = langbase.agent.run(
    input=[{"role": "user", "content": "Tell me about AI"}],
    model="openai:gpt-4.1-mini",
    api_key=llm_api_key,
)

print(response["output"])

4. Stream text (Simple)

form langbase import get_runner

# Stream text as it's generated
response = langbase.agent.run(
    input=[{"role": "user", "content": "Tell me about AI"}],
    model="openai:gpt-4.1-mini",
    api_key=llm_api_key,
    stream=True,
)

runner = get_runner(response)

for content in runner.text_generator():
    print(content, end="", flush=True)

5. Stream with typed events (Advanced)

from langbase import StreamEventType, get_typed_runner

response = langbase.agent.run(
    input=[{"role": "user", "content": "What is an AI Engineer?"}],
    model="openai:gpt-4.1-mini",
    api_key=llm_api_key,
    stream=True,
)

# Create typed stream processor
runner = get_typed_runner(response)

# Register event handlers
runner.on(
    StreamEventType.CONNECT,
    lambda event: print(f"✓ Connected! Thread ID: {event['threadId']}\n"),
)

runner.on(
    StreamEventType.CONTENT,
    lambda event: print(event["content"], end="", flush=True),
)

runner.on(
    StreamEventType.TOOL_CALL,
    lambda event: print(
        f"\n🔧 Tool call: {event['toolCall']['function']['name']}"
    ),
)

runner.on(
    StreamEventType.COMPLETION,
    lambda event: print(f"\n\n✓ Completed! Reason: {event['reason']}"),
)

runner.on(
    StreamEventType.ERROR,
    lambda event: print(f"\n❌ Error: {event['message']}"),
)

runner.on(
    StreamEventType.END,
    lambda event: print(f"⏱️  Total duration: {event['duration']:.2f}s"),
)

# Process the stream
runner.process()

Core Features

Pipes - AI Pipeline Execution

# List all pipes
pipes = langbase.pipes.list()

# Run a pipe
response = langbase.pipes.run(
    name="ai-agent",
    messages=[{"role": "user", "content": "Hello!"}],
    variables={"style": "friendly"},  # Optional variables
    stream=True,  # Enable streaming
)

Memory - Persistent Context Storage

# Create a memory
memory = langbase.memories.create(
    name="product-docs",
    description="Product documentation",
)

# Upload documents
langbase.memories.documents.upload(
    memory_name="product-docs",
    document_name="guide.pdf",
    document=open("guide.pdf", "rb"),
    content_type="application/pdf",
)

# Retrieve relevant context
results = langbase.memories.retrieve(
    query="How do I get started?",
    memory=[{"name": "product-docs"}],
    top_k=3,
)

Agent - LLM Agent Execution

# Run an agent with tools
response = langbase.agent.run(
response = langbase.agent.run(
    model="openai:gpt-4",
    messages=[{"role": "user", "content": "Search for AI news"}],
    tools=[{"type": "function", "function": {...}}],
    tool_choice="auto",
    api_key="your-llm-api-key",
    stream=True,
)

Tools - Built-in Utilities

# Chunk text for processing
chunks = langbase.chunker(
chunks = langbase.chunker(
    content="Long text to split...",
    chunk_max_length=1024,
    chunk_overlap=256,
)

# Generate embeddings
embeddings = langbase.embed(
embeddings = langbase.embed(
    chunks=["Text 1", "Text 2"],
    embedding_model="openai:text-embedding-3-small",
)

# Parse documents
content = langbase.parser(
content = langbase.parser(
    document=open("document.pdf", "rb"),
    document_name="document.pdf",
    content_type="application/pdf",
)

Examples

Explore the examples directory for complete working examples:

SDK Reference

For detailed SDK documentation, visit langbase.com/docs/sdk.

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Support

License

See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langbase-0.0.2.tar.gz (37.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langbase-0.0.2-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file langbase-0.0.2.tar.gz.

File metadata

  • Download URL: langbase-0.0.2.tar.gz
  • Upload date:
  • Size: 37.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.12

File hashes

Hashes for langbase-0.0.2.tar.gz
Algorithm Hash digest
SHA256 549e0cf50ec144f8751e4792b6307059b7515a0936d73a39488045b73278f401
MD5 870eae6cc9a822418cfb282a382c6034
BLAKE2b-256 829f9a013049557147a785118309f30c780a90f94b11bc2378eaa3e7ba4e37e1

See more details on using hashes here.

File details

Details for the file langbase-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: langbase-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.12

File hashes

Hashes for langbase-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 16c5f1fd6a284e24c89ac46590ac048fbab1aa5ae5e1c5a3a11e264a322bb636
MD5 da40232404fa1b90bc8c6b8868a3aa78
BLAKE2b-256 d166da382b4e27ae340adc7f4f77f400a8ea660d141c46aebaeade56b7db2fa8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page