Course ES
← back to chapter

Extract fields

Paste a messy blob — an email signature, a forwarded intro, a chat message —

Showcase — Extract fields

Paste a messy blob — an email signature, a forwarded intro, a chat message — and get back a strict JSON object: name, email, phone, company. The model fills what it finds and returns null for the rest instead of guessing, because the schema says those fields are optional, not absent.

Run

bash bootstrap-secrets.sh              # reads ../../../../.env, writes secrets/
docker compose up --build              # default: PROVIDER=openai

Open http://localhost:3000.

To run against Gemini instead:

PROVIDER=gemini docker compose up --build

What's where

  • backend/ai_openai.py — the Contact schema + responses.parse call
  • backend/ai_gemini.py — same schema, response_schema on the config
  • backend/main.py — identical FastAPI loader; reads PROVIDER and dispatches
  • frontend/app/page.tsx — textarea + result (rendered as JSON)
  • docker-compose.yml — two services, secrets mounted from ./secrets/

run(input: str) -> str returns the validated object serialized to pretty JSON, keeping the contract identical across the week's showcases.

Stop

docker compose down

Run locally

Download the project as a ZIP and run it with Docker. Brings up a FastAPI backend + Next.js frontend on localhost:3000.

Download extract-fields.zip

unzip extract-fields.zip
cd extract-fields
bash bootstrap-secrets.sh   # one-time: pulls API keys into ./secrets
docker compose up --build   # default provider: openai
# or:  PROVIDER=gemini docker compose up --build

Type some input, pick a provider, and run the same code shown in Source against the live API. Sign-in required.


  

The same modules the Run button hits. The whole project (frontend, Dockerfile, compose) is in the ZIP under README.

backend/ai_openai.py

"""Week 4 - Showcase 1 (OpenAI): pull contact fields out of free text.

A schema in, a clean object out. The model fills what it finds and leaves
the rest null instead of guessing. The run() contract still returns a
string, so we serialize the validated object to pretty JSON for display.
"""
import json
from typing import Optional

from openai import OpenAI
from pydantic import BaseModel

_client = OpenAI()


class Contact(BaseModel):
    name: Optional[str] = None
    email: Optional[str] = None
    phone: Optional[str] = None
    company: Optional[str] = None


def run(text: str) -> str:
    response = _client.responses.parse(
        model="gpt-5.4-nano",
        input="Extract the contact details from this text. Use null for "
              f"anything not present.\n\n{text.strip()}",
        text_format=Contact,
    )
    return json.dumps(response.output_parsed.model_dump(), indent=2)

backend/ai_gemini.py

"""Week 4 - Showcase 1 (Gemini): pull contact fields out of free text."""
import json
import os
from typing import Optional

from google import genai
from google.genai import types
from pydantic import BaseModel

_client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])


class Contact(BaseModel):
    name: Optional[str] = None
    email: Optional[str] = None
    phone: Optional[str] = None
    company: Optional[str] = None


def run(text: str) -> str:
    response = _client.models.generate_content(
        model="gemini-3.1-flash-lite",
        contents="Extract the contact details from this text. Use null for "
                 f"anything not present.\n\n{text.strip()}",
        config=types.GenerateContentConfig(
            response_mime_type="application/json",
            response_schema=Contact,
        ),
    )
    return json.dumps(response.parsed.model_dump(), indent=2)

Project files

  • .gitignore
  • README.md
  • backend/Dockerfile
  • backend/ai_gemini.py
  • backend/ai_openai.py
  • backend/main.py
  • backend/requirements.txt
  • bootstrap-secrets.sh
  • docker-compose.yml
  • frontend/Dockerfile
  • frontend/app/layout.tsx
  • frontend/app/page.tsx
  • frontend/next.config.ts
  • frontend/package.json
  • frontend/tsconfig.json