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.
Pixeltable’s OpenAI integration enables you to access OpenAI models via
the OpenAI API.
OpenAI usage may incur costs based on your OpenAI plan.
Be mindful of sensitive data and consider security measures when
integrating with external services.
First you’ll need to install required libraries and enter your OpenAI
API key.
%pip install -qU pixeltable openai
import getpassimport osif 'OPENAI_API_KEY' not in os.environ: os.environ['OPENAI_API_KEY'] = getpass.getpass( 'Enter your OpenAI API key:' )
Azure OpenAI
The same functions call Azure OpenAI. OPENAI_API_KEY is the Azure key,
and OPENAI_BASE_URL is the resource endpoint, passed through as the
client’s base_url. OPENAI_API_VERSION is sent as the api-version
query parameter; for the /openai/v1/ endpoint use preview or
latest. The model argument is the deployment name on that resource.
chat_completions(..., model='your-deployment') then uses that client.
The examples below are the same call, with an OpenAI model name or an
Azure deployment name.Now let’s create a Pixeltable directory to hold the tables for our demo.
import pixeltable as pxt# Remove the 'openai_demo' directory and its contents, if it existspxt.drop_dir('openai_demo', force=True)pxt.create_dir('openai_demo')
Created directory ‘openai_demo’.
<pixeltable.catalog.dir.Dir at 0x3108bfe90>
Chat completions
Create a Table: In Pixeltable, create a table with columns to represent
your input data and the columns where you want to store the results from
OpenAI.
from pixeltable.functions import openai# Create a table in Pixeltable and add a computed column that calls OpenAIt = pxt.create_table('openai_demo/chat', {'input': pxt.String})messages = [{'role': 'user', 'content': t.input}]t.add_computed_column( output=openai.chat_completions( messages=messages, model='gpt-4o-mini', model_kwargs={ # Optional dict with parameters for the OpenAI API 'max_tokens': 300, 'top_p': 0.9, 'temperature': 0.7, }, ))
Created table ‘chat’.
Added 0 column values with 0 errors in 0.00 s
No rows affected.
# Parse the response into a new columnt.add_computed_column(response=t.output.choices[0].message.content)
Added 0 column values with 0 errors in 0.00 s
No rows affected.
# Start a conversationt.insert( [{'input': 'How many islands are in the Aleutian island chain?'}])t.select(t.input, t.response).head()
Inserted 1 row with 0 errors in 1.68 s (0.60 rows/s)
Responses API
The Responses
API is
OpenAI’s recommended API for new projects. It offers built-in tools (web
search, file search, code interpreter), improved reasoning model
performance, lower costs, and cleaner semantics compared to Chat
Completions.Key benefits:
instructions parameter: Cleanly separates system-level guidance
from user input
Built-in tools: Use web_search, file_search,
code_interpreter natively
Multi-turn via previous_response_id: Simpler conversation state
management
# Create a table and add a computed column using the Responses APIresp_t = pxt.create_table('openai_demo/responses', {'input': pxt.String})messages = [{'role': 'user', 'content': resp_t.input}]resp_t.add_computed_column( output=openai.responses( input=messages, model='gpt-4o-mini', model_kwargs={ 'instructions': 'You are a helpful assistant. Be concise.', 'temperature': 0.7, 'max_output_tokens': 300, }, ))# The response text is the first content part of the first output itemresp_t.add_computed_column( response=resp_t.output.output[0].content[0].text)
Created table ‘responses’.
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.00 s
No rows affected.
# Insert data and view resultsresp_t.insert([{'input': 'What are the three largest moons of Jupiter?'}])resp_t.select(resp_t.input, resp_t.response).head()
Inserted 1 row with 0 errors in 1.93 s (0.52 rows/s)
Decisions
The Decisions
API answers
predicate, choice, and score questions about a text or image input. Each
answer is a probability, a choice, or a score instead of generated text.
decisions() requires openai>=3.32.This example checks food-delivery complaints against the customer’s
photo. A reply_prompt() UDF returns None unless the photo supports
the complaint, and a None input skips the responses() call, so only
supported complaints get a drafted reply.
@pxt.udfdef reply_prompt(complaint: str, supported: float | None) -> list | None: if supported is None or supported < 0.5: return None return [ { 'role': 'user', 'content': f'Draft a two-sentence apology with a refund for: {complaint}', } ]dec_t = pxt.create_table( 'openai_demo/complaints', {'complaint': pxt.String, 'photo': pxt.Image},)messages = [ { 'role': 'user', 'content': [ {'type': 'input_text', 'text': dec_t.complaint}, {'type': 'input_image', 'image_url': dec_t.photo}, ], }]questions = [ { 'type': 'predicate', 'name': 'supported', 'instructions': "Does the photo support the customer's complaint?", }, { 'type': 'choice', 'name': 'issue', 'instructions': 'What is the complaint about?', 'choices': [ {'value': 'missing item'}, {'value': 'unwanted item'}, {'value': 'food quality'}, {'value': 'other'}, ], }, { 'type': 'score', 'name': 'urgency', 'instructions': 'How upset is the customer?', 'levels': [ {'label': 'calm'}, {'label': 'annoyed'}, {'label': 'angry'}, ], },]dec_t.add_computed_column( triage=openai.decisions( messages, questions=questions, model='gpt-6-luna' ))dec_t.add_computed_column(supported=dec_t.triage.answers[0].probability)dec_t.add_computed_column(issue=dec_t.triage.answers[1].choice)dec_t.add_computed_column(urgency=dec_t.triage.answers[2].score)dec_t.add_computed_column( reply=openai.responses( reply_prompt(dec_t.complaint, dec_t.supported), model='gpt-4o-mini', ))dec_t.add_computed_column( reply_text=dec_t.reply.output[0].content[0].text)
Created table ‘complaints’.
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.00 s
Added 0 column values with 0 errors in 0.00 s
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.01 s
Added 0 column values with 0 errors in 0.01 s
No rows affected.
photo_url = 'https://raw.githubusercontent.com/pixeltable/pixeltable/release/docs/resources/images/000000000009.jpg'dec_t.insert( [ { 'complaint': 'I asked for no fruit, but the box came with oranges and pineapple. Not happy.', 'photo': photo_url, }, { 'complaint': 'My order is missing the broccoli I paid for!', 'photo': photo_url, }, ])
Inserted 2 rows with 0 errors in 2.04 s (0.98 rows/s)
2 rows inserted.
Inserted 1 row with 0 errors in 5.34 s (0.19 rows/s)
1 row inserted.
audio_t.head()
audio_t.head()[0]['result']['text']
‘Allow me to illustrate. During the last 60 days, I have been at the task of constructing an administration. It has been a long and deliberate process. Some have counseled greater speed. Others have counseled more expedient tests. But I have been guided by the standard John Winthrop set before his shipmates on the flagship Arabella 331 years ago, as they too faced the task of building a new government on a perilous frontier. We must always consider, he said, that we shall be as a city upon a hill. The eyes of all peoples are upon us. Today the eyes of all people are truly upon us. And our governments, in every branch, at every level,‘
Learn more
To learn more about advanced techniques like RAG operations in
Pixeltable, check out the RAG Operations in
Pixeltable
tutorial.
Last modified on October 30, 2026
Was this page helpful?
Assistant
Responses are generated using AI and may contain mistakes.