Skip to content

Cherry pick #123622 to 26.8: Send the query id with OpenAI AI function requests - #127872

Merged
robot-ch-test-poll merged 12 commits into
backport/26.8/123622from
cherrypick/26.8/123622
Oct 27, 2026
Merged

robot-ch-test-poll merged 12 commits into
backport/26.8/123622from
cherrypick/26.8/123622

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

Original pull-request #123622

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

george-larionov and others added 11 commits October 2, 2026 22:15
`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]>
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]>
…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]>
… 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]>
`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]>
Send the query id with OpenAI AI function requests
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request labels Oct 26, 2026
@george-larionov

Copy link
Copy Markdown
Member

@groeneai please fix the conflicts: keep 26.8's shape (call returns AIResponse, getHeaders(), embedTexts returns its result) 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;
  • the new query-id arguments to embedTexts.

Don't bring over the out-param and SCOPE_EXIT refactor.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator

Resolved as you described. Against backport/26.8/123622 the merge changes the same 11 files as #123622, and every changed line is #123622's except where 26.8's shape differs:

  • call keeps 26.8's signature and still returns AIResponse; only the request type becomes const AIChatRequest & (IAIProvider.h, OpenAIProvider.{h,cpp}, AnthropicProvider.{h,cpp}).
  • getHeaders() declarations and definitions are kept. In both OpenAIProvider request paths (chat and embedding) the getHeaders() loop stays and setCommonHeaders replaces the chassert + X-ClickHouse-AI-Function lines.
  • aiEmbed and aiSimilarity keep auto embedding_result = FunctionBaseAI::embedTexts(...) and only gain the two query-id arguments. No out-param, no SCOPE_EXIT.
  • The AIRequest base with query_id / initial_query_id, the AIChatRequest / AIEmbeddingRequest inheritance, the sanitizeForLog -> sanitize rename, the FunctionBaseAI changes and the two new integration tests merge cleanly.

Validated on the 26.8 tree with clang-22, debug build: clickhouse builds, and the whole test_ai_functions integration module passes locally (119 passed), including test_function_name_header, test_query_id_header and test_query_id_header_control_characters.

The merge is on my fork and its first parent is this PR's head, so applying it is a fast-forward:

git fetch https://github.com/groeneai/ClickHouse groeneai/resolve-26.8-cherrypick-123622
git push origin FETCH_HEAD:cherrypick/26.8/123622

(resolved head 27725b812709050ac606f4cf54c77430d684a24f)

@robot-ch-test-poll
robot-ch-test-poll merged commit 1485140 into backport/26.8/123622 Oct 27, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.8/123622 branch October 27, 2026 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants