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.
Parse and access specific fields from structured JSON responses returned
by language models.
Problem
LLM APIs return nested JSON responses with metadata you don’t need. You
want to extract just the text content or specific fields for downstream
processing.
{ "id": "chatcmpl-123", "choices": [{ "message": { "content": "This is the actual response text" // ← You want this } }], "usage": {"tokens": 50}}
Solution
What’s in this recipe:
Extract text content from chat completions
Access nested JSON fields
Create separate columns for different fields
You use JSON path notation to extract specific fields from API responses
and store them in computed columns.
Setup
%pip install -qU pixeltable openaiimport getpassimport osif 'OPENAI_API_KEY' not in os.environ: os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')
Create prompts table
import pixeltable as pxtfrom pixeltable.functions import openai# Create a fresh directorypxt.drop_dir('json_demo', force=True)pxt.create_dir('json_demo')
Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata
Created directory ‘json_demo’.
<pixeltable.catalog.dir.Dir at 0x313d7a150>
t = pxt.create_table('json_demo/prompts', {'prompt': pxt.String})
Created table ‘prompts’.
t.insert( [ {'prompt': 'What is the capital of France?'}, {'prompt': 'Write a haiku about coding'}, ])
# Extract just the text contentt.add_computed_column(text=t.response.choices[0].message.content)# Extract token usaget.add_computed_column(tokens=t.response.usage.total_tokens)
Added 2 column values with 0 errors.
Added 2 column values with 0 errors.
2 rows updated, 2 values computed.