Skip to content

The agent engineering platform.

cast to string when measuring token length (#8617)

Harrison Chase7c5c0557cbae
diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py
index de4108e069..7e1932165b 100644
--- a/libs/langchain/langchain/chat_models/openai.py
+++ b/libs/langchain/langchain/chat_models/openai.py
@@ -570,7 +570,9 @@ class ChatOpenAI(BaseChatModel):
for message in messages_dict:
num_tokens += tokens_per_message
for key, value in message.items():
- num_tokens += len(encoding.encode(value))
+ # Cast str(value) in case the message value is not a string
+ # This occurs with function messages
+ num_tokens += len(encoding.encode(str(value)))
if key == "name":
num_tokens += tokens_per_name
# every reply is primed with <im_start>assistant