---
title: "Using ClickHouse UDFs to integrate with OpenAI models"
description: "ClickHouse executable user-defined functions can call the OpenAI chat completions API from SQL, adding sentiment labels or extracted entities to rows at insert or query time."
url: "https://swe.quest/articles/using-clickhouse-udfs-to-integrate-with-openai-models-798f9ca64f94924a"
canonical: "https://clickhouse.com/blog/clickhouse-open-ai-user-defined-functions-udfs"
source: "ClickHouse"
published: "2023-09-14"
difficulty: "medium"
kind: "How-to"
reading_minutes: 17
tags: ["analytics","llms","sql","python"]
---

# Using ClickHouse UDFs to integrate with OpenAI models

A post on [ClickHouse](https://swe.quest/sources/clickhouse.md), published 2023-09-14, 17 min read, How-to. Original: [https://clickhouse.com/blog/clickhouse-open-ai-user-defined-functions-udfs](https://clickhouse.com/blog/clickhouse-open-ai-user-defined-functions-udfs)

## Summary

ClickHouse executable user-defined functions can call the OpenAI chat completions API from SQL, adding sentiment labels or extracted entities to rows at insert or query time. The walkthrough starts with a Bash script and then moves to a Python script with retries and exponential backoff, configured as an executable\_pool with chunk headers for parallel, batched invocation. Applying the UDF only to final result rows keeps API calls few, since OpenAI latency far exceeds ClickHouse query time. Aggregating sentiment across many rows means thousands of API calls, so rate limits and latency become the main constraints.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Analytics & OLAP](https://swe.quest/tags/analytics.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [SQL](https://swe.quest/tags/sql.md) (Languages)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- SQL basics ([easy posts on SQL](https://swe.quest/tags/sql.md?difficulty=easy))
- ClickHouse server configuration ([easy posts on Analytics & OLAP](https://swe.quest/tags/analytics.md?difficulty=easy))
- REST APIs ([easy posts on API design](https://swe.quest/tags/api-design.md?difficulty=easy))

## Keywords

ClickHouse, OpenAI, gpt-3.5-turbo, Executable UDFs, Hacker News dataset, tenacity

## Related posts

- [AI Functions in ClickHouse: Upgrade your SQL to the AI age](https://swe.quest/articles/ai-functions-in-clickhouse-upgrade-your-sql-to-the-ai-age-11cec6552950f306.md): ClickHouse · 2026-09-11 · Medium · 12 min read · LLMs, SQL, RAG & retrieval, Analytics & OLAP, Cost optimization
  ClickHouse's beta AI Functions call LLM and embedding providers directly from SQL.
- [Executable UDFs are now generally available on ClickHouse Cloud](https://swe.quest/articles/executable-udfs-are-now-generally-available-on-clickhouse-cloud-afc730ccd3e99d10.md): ClickHouse · 2026-10-05 · Medium · 20 min read · Analytics & OLAP, Rust, LLMs, Cost optimization, Observability
  Executable UDFs are now generally available on ClickHouse Cloud across AWS, GCP and Azure, adding a Native runtime for statically linked Rust/Go/C++/JavaScript binaries, outbound network access, per-process memory limits, a deterministic flag that enables the query cache, Cloud API and Terraform support, and per-query UDF metrics in 26.6+.
- [Tracing OpenAI agents with ClickStack](https://swe.quest/articles/tracing-openai-agents-with-clickstack-27a1630cee998ca8.md): ClickHouse · 2025-10-27 · Medium · 13 min read · Observability, AI agents, Python, Analytics & OLAP
  OpenAI's agents SDK has built-in tracing that can be redirected from OpenAI's hosted product to a custom TracingExporter.
- [How Fetch built its flexible agentic analytics tool on ClickHouse Cloud](https://swe.quest/articles/how-fetch-built-its-flexible-agentic-analytics-tool-on-clickhouse-cloud-284f56cdf33d7d26.md): ClickHouse · 2026-07-28 · Easy · 6 min read · Analytics & OLAP, AI agents, Data pipelines, LLMs
  Fetch built FAST, a conversational BI agent that lets non-technical sales teams query receipt purchase data in plain language, on ClickHouse Cloud ingesting 150 billion rows (6.5 TB compressed) a day.
- [Executable UDFs are now in public beta on ClickHouse Cloud](https://swe.quest/articles/executable-udfs-are-now-in-public-beta-on-clickhouse-cloud-89bd8753f4dda600.md): ClickHouse · 2026-05-30 · Medium · 14 min read · Analytics & OLAP, Inference & serving, Machine learning, SQL, Python
  Executable UDFs are in public beta on ClickHouse Cloud: you upload a zipped Python function with its model files, and ClickHouse runs it in a pool of long-lived sandboxed processes that can be called from SQL, including inside materialized views.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
