Python SDK for Laminar
Actively maintained. Last release 3 days ago, commits in 20 of the last 26 weeks. A safe choice.
pip install lmnrLaminar Python
Python SDK for Laminar.
Laminar is an open-source platform for engineering LLM products. Trace, evaluate, annotate, and analyze LLM data. Bring LLM applications to production with confidence.
Check our open-source repo and don't forget to star it ⭐
Quickstart
First, install the package, specifying the extras you want to use.
uv add lmnr
Initialize Laminar in your code:
from lmnr import Laminar
Laminar.initialize(project_api_key="<PROJECT_API_KEY>")
You can also skip passing the project_api_key, in which case it will be looked
in the environment (or local .env file) by the key LMNR_PROJECT_API_KEY.
Note that you need to only initialize Laminar once in your application. You should try to do that as early as possible in your application, e.g. at server startup.
Set-up for self-hosting
If you self-host a Laminar instance, the default connection settings to it are
http://localhost:8000 for HTTP and http://localhost:8001 for gRPC. Initialize
the SDK accordingly:
from lmnr import Laminar
Laminar.initialize(
project_api_key="<PROJECT_API_KEY>",
base_url="http://localhost",
http_port=8000,
grpc_port=8001,
)
Instrumentation
Manual instrumentation
To instrument any function in your code, we provide a simple @observe() decorator.
This can be useful if you want to trace a request handler or a function which combines multiple LLM calls.
import os
from openai import OpenAI
from lmnr import Laminar
Laminar.initialize(project_api_key=os.environ["LMNR_PROJECT_API_KEY"])
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def poem_writer(topic: str):
prompt = f"write a poem about {topic}"
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
# OpenAI calls are still automatically instrumented
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
)
poem = response.choices[0].message.content
return poem
@observe()
def generate_poems():
poem1 = poem_writer(topic="laminar flow")
poem2 = poem_writer(topic="turbulence")
poems = f"{poem1}\n\n---\n\n{poem2}"
return poems