Skip to main content

LangChain Opper

A LangChain integration package for Opper AI that provides chat models and provider utilities with seamless LangGraph compatibility.

Features

  • 🔗 LangChain Integration: Drop-in replacement for other LangChain chat models
  • 📊 Built-in Tracing: Automatic span creation and management using Opper's tracing
  • 📈 Metrics Support: Easy metric collection for evaluation and monitoring
  • 🏗️ Structured Outputs: Native support for Pydantic models with with_structured_output()
  • 🔧 Flexible Configuration: Customizable instructions, models, and task names
  • 🚀 Easy Setup: Simple installation and configuration

Installation

pip install langchain-opperai

Setup

Set your Opper API key as an environment variable:

export OPPER_API_KEY="your_opper_api_key_here"

Quick Start

Basic Chat Model

from langchain_opperai import ChatOpperAI
from langchain_core.messages import HumanMessage

# Initialize the chat model
llm = ChatOpperAI(
    task_name="chat",
    model_name="anthropic/claude-3.5-sonnet",
    instructions="You are a helpful AI assistant.",
)

# Use it like any LangChain chat model
messages = [HumanMessage(content="What is the capital of France?")]
result = llm.invoke(messages)
print(result.content)  # "The capital of France is Paris."

Structured Output

from langchain_opperai import ChatOpperAI
from pydantic import BaseModel, Field

class Joke(BaseModel):
    setup: str = Field(description="The joke setup")
    punchline: str = Field(description="The joke punchline")

# Create a structured model
llm = ChatOpperAI()
structured_llm = llm.with_structured_output(Joke)

# Get structured output
joke = structured_llm.invoke("Tell me a joke about cats")
print(f"Setup: {joke.setup}")
print(f"Punchline: {joke.punchline}")

Using the Provider

from langchain_opperai import OpperProvider
from pydantic import BaseModel, Field

class Response(BaseModel):
    answer: str = Field(description="The answer")
    confidence: float = Field(description="Confidence score 0-1")

# Initialize provider
provider = OpperProvider()

# Create models
chat_model = provider.create_chat_model(
    task_name="chat",
    instructions="You are a helpful assistant.",
)

structured_model = provider.create_structured_model(
    task_name="structured_chat",
    instructions="Provide structured responses.",
    output_schema=Response,
)

# With tracing
trace_id = provider.start_trace("conversation", "User asks about Python")
result = chat_model.invoke("What is Python?")
provider.end_trace("Provided Python explanation")

LangGraph Integration

from langgraph.graph import StateGraph, END
from langgraph.graph.message import add_messages
from langchain_core.messages import HumanMessage
from langchain_opperai import OpperProvider
from typing import TypedDict, Annotated

class State(TypedDict):
    messages: Annotated[list, add_messages]

def chat_node(state: State):
    provider = OpperProvider()
    llm = provider.create_chat_model(
        task_name="langgraph_chat",
        instructions="You are a helpful assistant in a graph workflow.",
    )
    
    response = llm.invoke(state["messages"])
    return {"messages": [response]}

# Build graph
workflow = StateGraph(State)
workflow.add_node("chat", chat_node)
workflow.set_entry_point("chat")
workflow.add_edge("chat", END)

app = workflow.compile()

# Run
result = app.invoke({
    "messages": [HumanMessage(content="Hello!")]
})

API Reference

ChatOpperAI

A LangChain chat model that integrates with Opper AI.

Parameters:

  • task_name (str): Name for the Opper task (default: "chat")
  • model_name (str): Model to use (default: "anthropic/claude-3.5-sonnet")
  • instructions (str): Instructions for the model
  • opper_client (Optional[Opper]): Opper client instance
  • parent_span_id (Optional[str]): Parent span ID for tracing

Methods:

  • with_structured_output(schema): Create a model that returns structured output
  • invoke(messages): Generate a response to the given messages
  • ainvoke(messages): Async version of invoke

OpperProvider

A provider class for creating Opper chat models and managing traces.

Methods:

  • create_chat_model(**kwargs): Create a new ChatOpperAI
  • create_structured_model(task_name, instructions, output_schema, **kwargs): Create a structured model
  • start_trace(name, input_data): Start a new trace
  • end_trace(output_data): End the current trace
  • add_metric(span_id, dimension, value, comment): Add a metric to a span

Environment Variables

  • OPPER_API_KEY: Your Opper API key (required)

Requirements

  • Python ≥ 3.9.2
  • langchain-core ≥ 0.3.0
  • opperai ≥ 1.0.0
  • pydantic ≥ 2.0.0

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Links

Metadata

Release files for langchain-opperai 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langchain-opperai 0.0.1
File Size Uploaded Provenance
langchain_opperai-0.0.1.tar.gz 8.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for langchain-opperai 0.0.1
File Interpreter ABI Platform
langchain_opperai-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / langchain_opperai-0.0.1.tar.gz

Download URL langchain_opperai-0.0.1.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
c62aaa80f21d5f5542865db694e8bf4141202db18f41abaf492c1390cc4dab15
BLAKE2b-256 checksum
How to use checksums
ee53fbb4018423c71866663621602d1d74962430a613bf2c29405e7dd7a81baa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.9.23 Linux/6.11.0-1018-azure

Release files / langchain_opperai-0.0.1-py3-none-any.whl

Download URL langchain_opperai-0.0.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea08b925d8f62dcf8d684a0c9f625512ce81fe39dd04efa27edd8133d693bb95
BLAKE2b-256 checksum
How to use checksums
c0e8f6b6d80b6d12cf8d9e575905035db2673198dae12122c98bb371cec8a38a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.4 CPython/3.9.23 Linux/6.11.0-1018-azure

Security for langchain-opperai 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page