DEV Community

Alex Spinov
Alex Spinov

Posted on

Marvin AI Has a Free API: The AI Engineering Toolkit That Makes LLMs Predictable

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")]
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

Why Marvin

  • One-linersmarvin.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
Enter fullscreen mode Exit fullscreen mode

Building AI features? Check out my AI tools or email spinov001@gmail.com.

Top comments (0)