Skip to content

SWF(bug): accept count_tokens requests without max_tokens - #73

Merged
maplepaladin73 merged 1 commit into
mainfrom
devin/1795397215-count-tokens-no-max-tokens
Nov 23, 2026
Merged

maplepaladin73 merged 1 commit into
mainfrom
devin/1795397215-count-tokens-no-max-tokens

Conversation

@maplepaladin73

@maplepaladin73 maplepaladin73 commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

POST /v1/messages/count_tokens now accepts requests without max_tokens, matching Anthropic's API, instead of returning 400.

Root cause: count_tokens deserialized the body into MessagesRequest, whose max_tokens field is required for generation. Axum's JSON extractor rejected otherwise valid counting requests with missing field 'max_tokens'.

Fix: a dedicated CountTokensRequest (model, messages, system, tools, tool_choice, thinking: the fields that affect the prompt) that lowers through the existing MessagesRequest → IR adapter with generation fields unset. The count still uses the same prompt rendering path as serving. /v1/messages still requires max_tokens.

Test plan

Regression test anthropic::handlers::tests::count_tokens_without_max_tokens sends the real route a request with no max_tokens, using a fixture tokenizer.

Before fix (on main):

test anthropic::handlers::tests::count_tokens_without_max_tokens ... FAILED
assertion `left == right` failed: {"type":"error","error":{"type":"invalid_request_error","message":"Invalid request JSON: Failed to deserialize the JSON body into the target type: missing field `max_tokens` at line 1 column 66"}}
  left: 400
 right: 200
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 3266 filtered out

After fix:

test anthropic::handlers::tests::count_tokens_without_max_tokens ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3266 filtered out

Full suite (ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo test -p spark-server --bin spark):

test result: ok. 3254 passed; 0 failed; 14 ignored; 0 measured; 0 filtered out; finished in 48.87s

A contract test, count_tokens_request_contract, checks that an empty messages list parses, that MessagesRequest still rejects a body without max_tokens, and that a body missing model or messages, or with the wrong messages type, is still rejected.

  • cargo fmt --all -- --check: pass
  • ATLAS_SKIP_BUILD=1 CUDARC_CUDA_VERSION=13000 cargo clippy -p spark-server --tests: pass, no warnings
  • bash scripts/check-license-headers.sh: 4267 valid, 0 invalid
  • typos: pass
  • Added tests
  • Not tested against a real model or GPU (CPU only)

Try it (against a running server):

curl -sS http://localhost:8080/v1/messages/count_tokens -H 'Content-Type: application/json' -d '{"model":"YOUR_MODEL","messages":[{"role":"user","content":"Hello"}]}'

Notes for reviewers

Untested: a live-model or Docker run; clippy ran on spark-server only, not the whole workspace (only this crate changed).


Software Factory opened this PR for Robert
Implemented in session Accept count_tokens requests without max_tokens
Review with Devin · Open in Devin Desktop


Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

@maplepaladin73
maplepaladin73 merged commit b08e7a9 into main Nov 23, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant