Skip to content

feat: add /tokenize endpoint for openai-compatible backend#2371

Merged
aabchoo merged 20 commits into
envoyproxy:mainfrom
hustxiayang:tokenize-openai
Jul 17, 2026
Merged

feat: add /tokenize endpoint for openai-compatible backend#2371
aabchoo merged 20 commits into
envoyproxy:mainfrom
hustxiayang:tokenize-openai

Conversation

@hustxiayang

@hustxiayang hustxiayang commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Description
This is split from #1650. Specifically this is to support tokenization for openai-compatible backend.

  Adds the /tokenize endpoint with the OpenAI-compatible (vLLM) backend:
  tokenize request/response schema, passthrough translator, endpoint spec
  wiring, tracing/metrics support, and docs. Model is required on the
  request union. Provider-specific backends follow in stacked PRs.

  Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

Signed-off-by: yxia216 <yxia216@bloomberg.net>
@hustxiayang
hustxiayang requested a review from a team as a code owner July 14, 2026 19:11
@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Jul 14, 2026
@hustxiayang hustxiayang changed the title feat: add /tokenize endpoint (OpenAI/vLLM base) feat: add /tokenize endpoint for openai-compatible backend Jul 14, 2026
@codecov-commenter

codecov-commenter commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.01923% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.84%. Comparing base (7113614) to head (bbd7182).

Files with missing lines Patch % Lines
internal/translator/tokenize.go 85.18% 4 Missing and 4 partials ⚠️
internal/apischema/openai/tokenize/tokenize.go 82.85% 3 Missing and 3 partials ⚠️
internal/endpointspec/endpointspec.go 81.81% 4 Missing ⚠️
internal/tracing/openinference/openai/tokenize.go 94.80% 2 Missing and 2 partials ⚠️
internal/tracing/tracer.go 66.66% 2 Missing and 1 partial ⚠️
internal/tracing/tracing.go 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2371      +/-   ##
==========================================
+ Coverage   84.82%   84.84%   +0.02%     
==========================================
  Files         148      151       +3     
  Lines       21878    22086     +208     
==========================================
+ Hits        18558    18739     +181     
- Misses       2198     2215      +17     
- Partials     1122     1132      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@hustxiayang

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for a new /tokenize endpoint compatible with the vLLM tokenize API, including request/response schemas, endpoint parsing, translation, metrics, tracing, and documentation. The reviewer feedback highlights a critical issue where boolean fields that default to true (such as AddSpecialTokens and AddGenerationPrompt) are defined as plain bool with omitzero tags, which would cause explicit false values to be omitted during marshaling. The reviewer recommends changing these fields to *bool pointers and provides the necessary updates for validation, tracing, and unit tests to safely handle the pointer dereferencing.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread internal/apischema/openai/tokenize/tokenize.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize.go
Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/apischema/openai/tokenize/tokenize_test.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize_test.go
Comment thread internal/tracing/openinference/openai/tokenize_test.go Outdated
Comment thread internal/tracing/openinference/openai/tokenize_test.go Outdated
Comment thread internal/endpointspec/endpointspec_test.go Outdated
Comment thread internal/translator/tokenize_test.go Outdated
hustxiayang and others added 13 commits July 14, 2026 15:57
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Signed-off-by: hustxiayang <yangxiast@gmail.com>
Signed-off-by: yxia216 <yxia216@bloomberg.net>
@hustxiayang

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for a new /tokenize endpoint compatible with the vLLM tokenize API, allowing users to count tokens for both chat and completion requests. It adds the necessary API schemas, endpoint parsing, metrics, OpenInference tracing, and translation logic, along with comprehensive tests and documentation. The review feedback highlights several key improvement opportunities, including strengthening request validation to handle default values for conflicting fields, adding nil checks to prevent potential panics, avoiding implicit struct field promotion in tracing attributes, and ensuring robust error translation when the Content-Type header is missing.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread internal/apischema/openai/tokenize/tokenize.go
Comment thread internal/apischema/openai/tokenize/tokenize.go
Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/translator/tokenize.go Outdated
@missBerg missBerg added enhancement New feature or request area/extproc ExtProc data-plane architecture, body limits, performance area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) labels Jul 15, 2026
Signed-off-by: yxia216 <yxia216@bloomberg.net>
@hustxiayang

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for a new /tokenize endpoint compatible with the vLLM tokenize API, including request/response schemas, endpoint specifications, OpenInference tracing, and translation logic. The review feedback highlights several improvement opportunities: optimizing JSON parsing in ParseBody by using a lightweight detector struct to avoid double unmarshaling, ensuring RequestUnion.Validate() triggers underlying ChatRequest validation, consistently accessing ChatRequest fields explicitly to prevent potential panics, and populating tokenUsage in ResponseBody to correctly track token usage metrics as documented.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread internal/endpointspec/endpointspec.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize.go
Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/translator/tokenize.go
Signed-off-by: yxia216 <yxia216@bloomberg.net>
@hustxiayang

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for a new /tokenize endpoint compatible with the vLLM tokenize API, allowing token counting for both chat messages and completion prompts. The changes span endpoint registration, API schema definitions, translation logic, metrics, tracing, and documentation. The review feedback highlights several opportunities to improve robustness by adding defensive nil checks for request, response, and body readers, as well as explicitly accessing embedded struct fields to avoid ambiguity.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/tracing/openinference/openai/tokenize.go
Comment thread internal/translator/tokenize.go
Comment thread internal/translator/tokenize.go
Comment thread internal/apischema/openai/tokenize/tokenize.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize_test.go
Comment thread internal/apischema/openai/tokenize/tokenize_test.go
Comment thread internal/apischema/openai/tokenize/tokenize_test.go
Comment thread internal/apischema/openai/tokenize/tokenize_test.go
Comment thread internal/apischema/openai/tokenize/tokenize.go Outdated
Comment thread internal/apischema/openai/tokenize/tokenize_test.go Outdated
Signed-off-by: yxia216 <yxia216@bloomberg.net>
@hustxiayang

Copy link
Copy Markdown
Contributor Author

/retest

@aabchoo aabchoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall looks good to me!

@aabchoo
aabchoo merged commit ea1c886 into envoyproxy:main Jul 17, 2026
55 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/extproc ExtProc data-plane architecture, body limits, performance area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) enhancement New feature or request size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants