How to beginner · 3 min read

How to use LLM for form filling

Quick answer
Use a large language model (LLM) like gpt-4o-mini to generate structured JSON outputs for form filling by prompting it with clear instructions and example schemas. Call client.chat.completions.create with a system prompt defining the form structure and parse the JSON response to fill your form fields automatically.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python SDK and set your API key as an environment variable.

  • Run pip install openai to install the SDK.
  • Set your API key in your shell: export OPENAI_API_KEY='your_api_key_here' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key_here" (Windows).
bash
pip install openai

Step by step

Use the gpt-4o-mini model to generate JSON-formatted form data by providing a system prompt that defines the form schema and a user prompt with the input data. Parse the JSON response to fill your form fields.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

system_prompt = (
    "You are a form-filling assistant. Generate a JSON object with the following fields:"
    " name (string), email (string), phone (string), and age (integer)."
    " Respond ONLY with a valid JSON object without extra text."
)

user_input = "Name: John Doe, Email: john.doe@example.com, Phone: 555-1234, Age: 30"

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": user_input}
    ]
)

json_output = response.choices[0].message.content
print("Filled form JSON:", json_output)
output
Filled form JSON: {"name": "John Doe", "email": "john.doe@example.com", "phone": "555-1234", "age": 30}

Common variations

You can use other models like claude-3-5-haiku-20241022 with the Anthropic SDK or add validation by parsing the JSON output with json.loads(). For asynchronous calls, use async client methods if supported. You can also prompt for nested or more complex form structures by expanding the system prompt.

python
import json
from anthropic import Anthropic

client = Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system = "You are a form-filling assistant. Return JSON with fields: name, email, phone, age."
messages = [{"role": "user", "content": "Name: Jane Smith, Email: jane@example.com, Phone: 555-6789, Age: 25"}]

response = client.messages.create(
    model="claude-3-5-haiku-20241022",
    max_tokens=500,
    system=system,
    messages=messages
)

json_text = response.content[0].text
form_data = json.loads(json_text)
print(form_data)
output
{'name': 'Jane Smith', 'email': 'jane@example.com', 'phone': '555-6789', 'age': 25}

Troubleshooting

  • If the model returns text outside JSON, instruct it explicitly to respond with only JSON in the system prompt.
  • If JSON parsing fails, validate the output string or use regex to extract JSON.
  • Check your API key environment variable if authentication errors occur.

Key Takeaways

  • Use clear system prompts to define the exact JSON schema for form filling.
  • Parse the LLM's JSON output programmatically to automate form population.
  • Validate and sanitize the model output to handle formatting inconsistencies.
  • Anthropic and OpenAI both support structured output generation with their latest models.
  • Always keep your API keys secure and use environment variables for authentication.
Verified 2026-04 · gpt-4o-mini, claude-3-5-haiku-20241022
Verify ↗