Skip to content

Send the query id with OpenAI AI function requests - #123622

Merged
george-larionov merged 10 commits into
masterfrom
ai-functions/send-query-id
Oct 14, 2026
Merged

george-larionov merged 10 commits into
masterfrom
ai-functions/send-query-id

Conversation

@george-larionov

@george-larionov george-larionov commented Oct 2, 2026 •

Copy link
Copy Markdown
Member

OpenAIProvider (used by the openai, huggingface and tei providers) now sends two headers with every AI function request (aiGenerate, aiClassify, aiExtract, aiTranslate, aiEmbed, aiSimilarity), next to the existing X-ClickHouse-AI-Function header:

  • X-ClickHouse-Query-Id: the query_id of the query that makes the request. On a remote shard of a distributed query it is the shard's own query id; for a background mutation it is the mutation task's id.
  • X-ClickHouse-Initial-Query-Id: the initial_query_id, the same on all shards of a distributed query. Not sent for a background mutation, which has no initial query.

The initial query id lets the endpoint group the requests of one user query. ASCII control characters in both values are replaced with spaces, because query ids from the native and gRPC protocols are not sanitized. The anthropic provider does not send these headers.

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

AI functions using the openai, huggingface or tei provider send the query id and the initial query id in the X-ClickHouse-Query-Id and X-ClickHouse-Initial-Query-Id HTTP headers.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/123622]

Version info

  • Merged into: 26.10.1.2391-master (included in 26.10 and later)
  • Backported to: 26.9.26.22, 26.8.35.20

`OpenAIProvider` sends `X-ClickHouse-Query-Id` with `initial_query_id`, or
`current_query_id` for a background mutation, which has no initial query.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [120570e]

Summary: ✅


AI Review

Summary

PR #123622 adds the current and initial query IDs to AI requests using the openai, huggingface, and tei providers, with ASCII control characters replaced by spaces. Reviewed head 120570ecdb99a053bc9baffaa2ffab86f1a53962: no actionable findings remain after checking the current diff, surrounding execution paths, and every prior review reply.

Missing context / blind spots
  • ⚠️ Current-head integration results are not available yet, and no tests were run locally. Both test_query_id_header and test_query_id_header_control_characters passed on 2ebf7765a8a426c4580e941e1ea4afadd49adbc4, including distributed queries and background mutations (CI report). The tests, ID propagation, and header sanitization are unchanged; the intervening merge changed the providers' HTTP transport helpers. Successful current-head integration results would close this remaining validation gap.
Final Verdict

Status: ✅ Approve. No new inline findings or unresolved review concerns require changes.

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Oct 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit 120570ecdb99a053bc9baffaa2ffab86f1a53962 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception).

See the job log for details.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is wrong to send initial_query_id instead of a query_id. Why?

The actual query_id on shards provides more precise information. Initial query_id can be reconstructed from it. For the purpose of distributed tracing, it is important to know what exact server has made a request. This is also important for audit purposes. This is also worth noting that you've named the header as Query-Id, and nothing tells that it is the initial query id - so it would lead to confusion. It is also inconsistent with all other cases of where the query_id is sent or exposed.

@alexey-milovidov

Copy link
Copy Markdown
Member

Everything else LGTM.

The `X-ClickHouse-Query-Id` header now carries the query id of the query
that makes the request. On a remote shard it is the shard's own query id,
which identifies the exact server that made the request and from which the
`initial_query_id` can be reconstructed via `system.query_log`. This is also
consistent with how the query id is exposed elsewhere. A background mutation
sends its task's `current_query_id`, as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

This comment was marked as low quality.

Comment thread src/Functions/AI/OpenAIProvider.cpp Resolved
…m query id headers

The initial query id lets the endpoint group the requests of a distributed query.
Query ids from the native and gRPC protocols are not sanitized, so control
characters could inject extra headers.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@george-larionov

Copy link
Copy Markdown
Member Author

@alexey-milovidov the inference team needs this query ID for debugging customer issues, and the customer won't know their distributed query_ids, they will only know the initial ID. See this Slack thread. I'm going to add both in this PR if the query_id is also needed, see my next commit.

@alexey-milovidov

This comment was marked as low quality.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 7, 2026
@rschu1ze
rschu1ze removed this pull request from the merge queue due to a manual request Oct 7, 2026
Comment thread src/Functions/AI/IAIProvider.h Resolved
Comment thread src/Functions/AI/IAIProvider.h Resolved
Comment thread src/Functions/AI/IAIProvider.h Resolved
Comment thread src/Functions/AI/OpenAIProvider.cpp Resolved
Comment thread src/Functions/AI/OpenAIProvider.cpp Resolved
alexey-milovidov and others added 3 commits October 7, 2026 10:43
… only `query_id`

- Introduce a base struct `AIRequest` with the fields common to chat and embedding
  requests (`function_name`, `query_id`); the chat request is renamed to `AIChatRequest`
  and both it and `AIEmbeddingRequest` inherit from the base.
- Sanitize the `X-ClickHouse-Query-Id` header with the existing `sanitizeForLog`
  (control characters become spaces) instead of a duplicated helper.
- Drop the `X-ClickHouse-Initial-Query-Id` header: the shard's own `query_id` is
  sufficient, and the initial query id can be reconstructed from it.
- Remove redundant comments.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@rschu1ze rschu1ze added the hold Skip for auto-updating. label Oct 7, 2026
Comment thread src/Functions/AI/OpenAIProvider.cpp Resolved
rschu1ze and others added 2 commits October 7, 2026 13:27
`X-ClickHouse-Query-Id` carries the current query id, `X-ClickHouse-Initial-Query-Id`
the initial one, which is the same on all shards of a distributed query.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Functions/AI/OpenAIProvider.cpp Resolved
@rschu1ze rschu1ze self-assigned this Oct 13, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The only red check, Stateless tests (arm_asan_ubsan, azure, sequential), is a job-level timeout: the job hit its 9000-second limit at test 1158 of 1180, and every test that ran passed (report). This PR doesn't cause it. It is tracked by #123849, which splits that job into 2 batches. I merged the latest master (it builds cleanly) and resolved the review thread about the PR description, which the author has already updated.

@george-larionov
george-larionov added this pull request to the merge queue Oct 14, 2026
Merged via the queue into master with commit 555235b Oct 14, 2026
350 of 351 checks passed
@george-larionov
george-larionov deleted the ai-functions/send-query-id branch October 14, 2026 16:46
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 14, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 26, 2026
robot-ch-test-poll added a commit that referenced this pull request Oct 27, 2026
Cherry pick #123622 to 26.8: Send the query id with OpenAI AI function requests
robot-ch-test-poll added a commit that referenced this pull request Oct 27, 2026
Cherry pick #123622 to 26.9: Send the query id with OpenAI AI function requests
george-larionov pushed a commit that referenced this pull request Oct 27, 2026
Merge backport/26.8/123622 into cherrypick/26.8/123622. The conflicts come
from 26.8's provider shape (`call` returns `AIResponse`, `getHeaders()`,
`embedTexts` returns its result), which differs from master's. Keep that
shape and add only what #123622 introduces: the `AIRequest` base struct with
`query_id` / `initial_query_id` and the `AIChatRequest` rename,
`setCommonHeaders` in place of the manual `X-ClickHouse-AI-Function` line,
and the query-id arguments to `embedTexts`. The out-param / `SCOPE_EXIT`
refactor from master is not brought over.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse robot-clickhouse added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 27, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 27, 2026
Backport #123622 to 26.9: Send the query id with OpenAI AI function requests
clickhouse-gh Bot added a commit that referenced this pull request Oct 27, 2026
Backport #123622 to 26.8: Send the query id with OpenAI AI function requests
leshikus pushed a commit that referenced this pull request Nov 13, 2026
leshikus pushed a commit that referenced this pull request Nov 13, 2026
@clickhouse-gh clickhouse-gh Bot added comp-ai-functions AI functions: SQL functions that call a large language model or an embedding model (aiGenerate, a... and removed comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-ai-functions AI functions: SQL functions that call a large language model or an embedding model (aiGenerate, a... pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-improvement Pull request with some product improvements pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants