Skip to content

[BUG CLIENT]: Base64 url counting towards token limit when using Document QnA #502

@gjpower

Description

@gjpower

Python -VV

Python 3.13.7 (main, Feb 20 2026, 12:03:52) [GCC 11.4.0]

Pip Freeze

annotated-types==0.7.0
anyio==4.13.0
certifi==2026.4.22
eval_type_backport==0.3.1
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.13
importlib_metadata==8.7.1
jsonpath-python==1.1.5
mistralai==2.4.2
opentelemetry-api==1.39.1
opentelemetry-semantic-conventions==0.60b1
pydantic==2.13.3
pydantic_core==2.46.3
python-dateutil==2.9.0.post0
six==1.17.0
typing-inspection==0.4.2
typing_extensions==4.15.0
zipp==3.23.1

Reproduction Steps

Follow base64 document QnA example illustrated here https://docs.mistral.ai/studio-api/document-processing/document_qna?tab=qna-base64-encoded-pdf#explorer-tabs-qna-usage

With a single page 308 kB test pdf the following error is produced.

mistralai.client.errors.sdkerror.SDKError: API error occurred: Status 400. Body: {"object":"error","message":"Prompt contains 321774 tokens and 0 draft tokens, too large for model with 262144 maximum context length","type":"invalid_request_invalid_args","param":null,"code":"3051","raw_status_code":400}

With a smaller 128 kB test pdf no error is produced but a very high token count is returned

ChatCompletionResponse(id='42c98cab821c46729d0e7e756847fafd', object='chat.completion', model='mistral-small-latest', usage=UsageInfo(prompt_tokens=133996, completion_tokens=21, total_tokens=134017, prompt_audio_seconds=Unset(), prompt_tokens_details={'cached_tokens': 0}), created=1777286265, choices=[ChatCompletionChoice(index=0, finish_reason='stop', message=AssistantMessage(role='assistant', content='Here is more text to use as an example it will just be used to take up some space.', tool_calls=None, prefix=False), messages=None)])

The word count of the text in this pdf is 455 words

Expected Behavior

The maximum context length not to be exceeded and the token count to correspond relative to the number of tokens in the underlying text in the pdf after ocr.
It seems currently the base64 url is being included in the token count in some way

Additional Context

This seems more likely a server or model side issue but occurs with various models so is not being submitted as a model specific bug

Suggested Solutions

Check how base64 urls are being handled and if they are contributing to the token count

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions