Repository navigation
types: keep the server's bytes field on logprobs - #782
Open
Uneadlight wants to merge 1 commit into
Open
Uneadlight wants to merge 1 commit into
Uneadlight wants to merge 1 commit into
Conversation
When logprobs=True the server returns `bytes` (the token's raw UTF-8
bytes) on every entry of `logprobs` and `top_logprobs`. `TokenLogprob`
only declared token/logprob, so pydantic's default `extra='ignore'`
dropped the field without any error.
Add `bytes: Optional[Sequence[int]] = None` to `TokenLogprob`, which
`Logprob` inherits. It stays a list of ints (not `bytes`) so
`model_dump()` remains JSON-serializable.
Verified against a live server with qwen2.5:3b:
/api/generate (logprobs=True)
-> {token: Hello, logprob: -0.20, bytes: [72, 101, 108, 108, 111]}
client before this change
-> AttributeError: 'Logprob' object has no attribute 'bytes'
client after this change
-> [72, 101, 108, 108, 111]
Older servers that omit the field still parse, with None by default and
no key in `model_dump(exclude_none=True)`, matching the backward
compatible behavior requested in ollama#750.
Tests: 4 added (generate, chat, optional field, top_logprobs).
Fixes ollama#750
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
TokenLogprobnow keeps thebytesfield the server already sends.Why
With
logprobs=True,/api/generateand/api/chatreturn abytesarray (thetoken's raw UTF-8 bytes) on every entry of
logprobsandtop_logprobs:{"token": "Hello", "logprob": -0.20118224620819092, "bytes": [72, 101, 108, 108, 111]}TokenLogprobonly declaredtokenandlogprob, and sinceSubscriptableBaseModeldoes not setextra='allow', pydantic's defaultextra='ignore'dropped the field without any error:After the change:
How
Add one optional field to
TokenLogprob(Logprobinherits it):It is kept as a list of ints rather than a
bytesobject somodel_dump()stays JSON-serializable.
Older servers that do not send the field still parse: the attribute is
Noneandmodel_dump(exclude_none=True)omits it, which is the backward-compatiblebehavior requested in #750.
Tests
4 added to
tests/test_type_serialization.py:test_generate_response_keeps_logprob_bytestest_chat_response_keeps_logprob_bytestest_logprob_bytes_is_optionaltest_top_logprobs_keep_bytespytest tests: 113 passed, 8 failed. The 8 failures are pre-existing andunrelated (
test_client_create_blob*hitsPermissionErroron this machine andthe image serialization tests fail with
Invalid image datadue to a broken%TEMP%); the same 8 fail onmainwithout this change.ruff checkandruff format --check: clean.Fixes #750