feat(api): add /v1/detokenize endpoint
Summary
Closes #1649. Mirror of /v1/tokenize for the inverse direction: take a list of token IDs and return the detokenized text, requested by @benniekiss in the issue thread for "complete API workflow" use cases that need to turn token IDs back into text without local processing.
The proto/handler shape was discussed in https://github.com/mudler/LocalAI/issues/1649#issuecomment-4344482467. @benniekiss reacted positively; landing this with the strict-mirror-of-tokenize precedent in mind. Happy to adjust if the proto naming or response shape should differ.
What's added
- Proto (
backend/backend.proto): newDetokenize(DetokenizeRequest) returns (DetokenizeResponse)RPC, withDetokenizeRequest{repeated int32 tokens}andDetokenizeResponse{string content}. The Go bindings are regenerated bymake protogen-go(gitignored as usual). - llama.cpp backend (
backend/cpp/llama-cpp/grpc-server.cpp): handler that callscommon_token_to_pieceper token and concatenates — the same primitiveTokenizeStringalready uses internally at the same file. - Other backends: inherit the default
Unimplementedfrompkg/grpc/base.Base— same pattern asDetect,Rerank, etc. Backends can opt in later. - Go plumbing:
pkg/grpc/{interface,server,backend,client,embed}.go+pkg/grpc/base/base.goupdated alongside theirTokenizeStringcounterparts. - HTTP:
POST /v1/detokenizeincore/http/endpoints/localai/detokenize.goandcore/http/routes/localai.go. Request{"model": "...", "tokens": [...]}, response{"content": "..."}. - Auth: entry in
RouteFeatureRegistrygated by the existingFeatureTokenize— no new feature flag. - Discovery: added under
ai_functionsin the routes index. - Swagger regenerated;
authentication.mdupdated to list the new endpoint.
Test plan
- [x]
make protogen-goregenerates clean - [x]
go build ./core/... ./pkg/grpc/...clean - [x]
go vet ./core/... ./pkg/grpc/...clean - [x]
go test -c -o /dev/null ./core/services/nodes/...clean (the existing testcontainers-based suite needs Docker; only updated the two interface mocks so the test package still compiles) - [x]
make swaggerregenerates with the new endpoint visible - [ ] Manual round-trip:
POST /v1/tokenize→POST /v1/detokenizereturns the original text on a llama.cpp model
Assisted-by: Claude:claude-opus-4-7
Rebased onto current master (05e8e1e9). The only conflicts were in the generated swagger files — upstream had added Diarization types; I merged both sets in alphabetical order. All other files applied cleanly. Ready for another look when you have a moment.
Hi @mudler — just checking in on this one. The e2e mock backend tests you requested are in (commit 349c9d2), and the branch was rebased onto current master on May 21. Happy to address any further feedback whenever you get a chance to take another look.
Rebased onto current master — branch now has 2 commits (the endpoint + e2e tests) cleanly on top of master with no unrelated changes.
Also checked: no other PR has landed a /v1/detokenize implementation in master. The feature is still unaddressed upstream (related open issue: #1649), so this PR is still relevant.
Hi @mudler — just flagging that the e2e tests requested in the review have been added in the second commit (c168bcba). The test suite adds a Detokenize method to the mock gRPC backend and covers both a basic POST with known token IDs and a tokenize→detokenize round-trip.
Happy to make any further changes if needed.