Skip to content

Fix crash counting messages with null content (tool-call logs) - #238

Open
eeshsaxena wants to merge 1 commit into
AgentOps-AI:mainfrom
eeshsaxena:fix/count-message-tokens-none-content
Open

Fix crash counting messages with null content (tool-call logs)#238
eeshsaxena wants to merge 1 commit into
AgentOps-AI:mainfrom
eeshsaxena:fix/count-message-tokens-none-content

Conversation

@eeshsaxena

Copy link
Copy Markdown

count_message_tokens (and therefore calculate_prompt_cost) crashes on a normal OpenAI message where content is null:

from tokencost import calculate_prompt_cost
messages = [
    {"role": "user", "content": "What is the weather?"},
    {"role": "assistant", "content": None},  # assistant turn that only carries tool_calls
]
calculate_prompt_cost(messages, "gpt-4o")
# TypeError: expected string or buffer

OpenAI's chat schema makes content nullable, and an assistant message that only carries tool_calls has content: null. The counting loop passes every field value straight into encoding.encode(value), which raises TypeError on None. So any pipeline that estimates cost over real conversation logs with tool calls hits this.

A null field adds no text tokens, so the fix skips None values. Counting a null content now matches counting an empty string. Added a test.

OpenAI's chat schema allows content to be null, most commonly on an assistant
message that only carries tool_calls. count_message_tokens passed every field
value straight to encoding.encode(), so a None value raised
TypeError: expected string or buffer, taking down calculate_prompt_cost with it.
Skip None values (they add no text tokens) and add a test.
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