This documentation page is also available as an interactive notebook. You can launch the notebook in
Kaggle or Colab, or download it for use with an IDE or local Jupyter installation, by clicking one of the
above links.
Use AI vision to extract JSON data from receipts, forms, documents, and
other images.
Problem
You have images containing structured information (receipts, forms, ID
cards) and need to extract specific fields as JSON for downstream
processing.
Solution
What’s in this recipe:
- Extract structured JSON from images using GPT-4o
- Use
openai.chat_completions() with multimodal messages
- Access individual fields from the extracted data
You use Pixeltable’s openai.chat_completions() function with
multimodal messages that include images directly. Request JSON output
via response_format in model_kwargs.
Setup
Load images
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/asiegel/.pixeltable/pgdata
Created directory ‘extraction_demo’.
<pixeltable.catalog.dir.Dir at 0x1232b4f70>
Created table ‘images’.
Inserted 2 rows with 0 errors in 0.03 s (60.43 rows/s)
2 rows inserted.
Use openai.chat_completions() to analyze images and get JSON output:
Added 2 column values with 0 errors in 7.55 s (0.26 rows/s)
2 rows updated.
Explanation
Getting JSON output:
Pass model_kwargs={'response_format': {'type': 'json_object'}} to get
structured JSON.
Other extraction use cases:
See also