Skip to content

polars-llm

PyPI version Python versions Build status License

Call OpenAI, Anthropic, and Gemini models from a Polars DataFrame, one row at a time, using native Polars expressions.

polars-llm registers an .llm namespace on Polars expressions so you can call any LangChain-supported chat model or embedding model on every row of a DataFrame — synchronously or asynchronously — and pipe the responses straight back into your data pipeline.

import polars as pl
import polars_llm  # noqa: F401  — registers the `.llm` namespace

(
    pl.DataFrame({"user_prompt": ["Summarise polars in one sentence."]})
      .with_columns(
          pl.col("user_prompt").llm.openai(model="gpt-4o-mini").alias("answer")
      )
)

Why polars-llm?

  • Expression-native — works inside with_columns, select, and any other Polars expression context.
  • Sync and asyncaopenai, aanthropic, agemini fan out concurrently with asyncio.gather.
  • Per-row prompts and system messages — every argument can be a Polars expression.
  • Structured outputs — pass a Pydantic schema as schema= and get a struct column back.
  • Embeddingsopenai_embed and gemini_embed return List[Float64] columns.
  • Powered by LangChain.

Install

pip install "polars-llm[openai]"
pip install "polars-llm[anthropic]"
pip install "polars-llm[gemini]"
pip install "polars-llm[all]"

Requires Python 3.9+ and Polars 1.0+. Auth follows LangChain conventions: set OPENAI_API_KEY, ANTHROPIC_API_KEY, or GOOGLE_API_KEY before importing.

Quickstart

Chat per row

df = (
    pl.DataFrame({"user_prompt": ["Capital of Spain?", "Capital of France?"]})
      .with_columns(
          pl.col("user_prompt").llm.openai(model="gpt-4o-mini").alias("answer")
      )
)

Structured output

from pydantic import BaseModel

class Sentiment(BaseModel):
    label: str
    confidence: float

df.with_columns(
    pl.col("review").llm.openai(model="gpt-4o-mini", schema=Sentiment).alias("s")
).unnest("s")

Embeddings

df.with_columns(
    pl.col("text").llm.openai_embed(model="text-embedding-3-small").alias("vector")
)

See the full API reference.