Repolode
google

google/langfun

A Python library for working with LLMs through objects and types, letting developers prompt models and get structured outputs.

Stars
919
Forks
84
Open issues
52
Last commit
yesterday

What is it?

Langfun is a Python library built on PyGlove that applies object-oriented principles to prompting language models. Instead of writing free-form text and parsing the response by hand, developers define Python classes and pass them as the expected output type, so the model's answer is returned as a typed object.

The library provides a unified API across several LLM providers, including Gemini, GPT and Claude, and supports modular prompts that mix text with other modalities such as images. It also covers batch jobs as well as request-based workflows, and includes an eval framework for running prompts across many input dimensions.

A basic call uses the lf.query function with a prompt template, an output type and a language model object. Optional extras add VertexAI access, MIME and image support, and UI enhancements.

Key features

  • 01Returns model outputs as typed Python objects defined with PyGlove classes.
  • 02Provides a unified API for Gemini, GPT, Claude and other LLMs.
  • 03Supports modular prompts that combine text with images and other modalities.
  • 04Handles both interactive requests and batch jobs.
  • 05Includes an eval framework for running prompts across many input dimensions.
  • 06Installs minimally, with optional extras for VertexAI, MIME and UI features.

Use cases

  • Extracting structured data from text or images into Python objects.
  • Building LLM-powered agents or pipelines inside an existing Python codebase.
  • Running batch prompts over large datasets and collecting typed results.
  • Evaluating prompt and model variants across many input combinations.

Quick start

  1. 1

    Install Langfun with the minimal dependencies

    pip install langfun
  2. 2

    Install Langfun with all dependencies for a complete setup

    pip install langfun[all]
  3. 3

    Run the Hello, Langfun example with your OpenAI API key (replace the placeholder key)

Things to know

  • Calling hosted models like Gpt4o requires a paid API key from the provider.
  • The project disclaims that it is not an officially supported Google product.

In collections

Topics

FAQ

What is langfun?
A Python library for working with LLMs through objects and types, letting developers prompt models and get structured outputs. Langfun is a Python library built on PyGlove that applies object-oriented principles to prompting language models. Instead of writing free-form text and parsing the response by hand, developers define Python classes and pass them as the expected output type, so the model's answer is returned as a typed object.
Is langfun free to use? What license does it have?
langfun is open source under the Apache-2.0 license. Check the license terms before using it in commercial products.
Is langfun still maintained?
Actively maintained. The last commit to langfun was on Nov 6, 2026.
What can I use langfun for?
Extracting structured data from text or images into Python objects. Building LLM-powered agents or pipelines inside an existing Python codebase. Running batch prompts over large datasets and collecting typed results. Evaluating prompt and model variants across many input combinations.
How do I install langfun?
Install Langfun with the minimal dependencies: pip install langfun Install Langfun with all dependencies for a complete setup: pip install langfun[all] Run the Hello, Langfun example with your OpenAI API key (replace the placeholder key)
Where can I get langfun?
The source code and installation instructions are on GitHub: https://github.com/google/langfun

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects