LLMs are unpredictable. Marvin makes them predictable — with typed functions, classifiers, and extractors that return structured data.
What Is Marvin?
Marvin is a Python toolkit for building AI-powered features. Instead of wrestling with prompts, you use high-level abstractions that return typed Python objects.
import marvin
# Classify text into categories
result = marvin.classify(
"I can't believe they charged me twice!",
labels=["billing", "technical", "account", "shipping"]
)
print(result) # "billing"
# Extract structured data
from pydantic import BaseModel
class Location(BaseModel):
city: str
state: str
country: str
locations = marvin.extract(
"I flew from San Francisco to New York, then London",
target=Location
)
# [Location(city="San Francisco", state="CA", country="US"),
# Location(city="New York", state="NY", country="US"),
# Location(city="London", state="", country="UK")]
AI Functions
@marvin.fn
def sentiment(text: str) -> float:
'''Returns sentiment score from -1 (negative) to 1 (positive)'''
score = sentiment("This product is amazing!") # 0.95
score = sentiment("Worst purchase ever") # -0.85
@marvin.fn
def translate(text: str, target_language: str) -> str:
'''Translates text to the target language'''
result = translate("Hello world", "Spanish") # "Hola mundo"
The docstring IS the prompt. The type hints define the output format. Marvin handles the rest.
Image Analysis
img = marvin.Image("receipt.jpg")
total = marvin.extract(img, target=float, instructions="total amount on receipt")
items = marvin.extract(img, target=list[str], instructions="list of items purchased")
Why Marvin
-
One-liners —
marvin.classify(),marvin.extract(),marvin.cast() - Type-safe — Pydantic models for structured output
- AI functions — docstring = prompt, type hints = output format
- Vision — extract data from images
-
Casting — transform data between types:
marvin.cast("twelve", int)→12
pip install marvin
Building AI features? Check out my AI tools or email spinov001@gmail.com.
Top comments (0)