From 7c5c0557cbae2c645d77d83c355b9817e7a554fd Mon Sep 17 00:00:00 2001 From: Harrison Chase Date: Wed, 2 Aug 2023 00:12:59 -0700 Subject: [PATCH] cast to string when measuring token length (#8617) --- libs/langchain/langchain/chat_models/openai.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/chat_models/openai.py b/libs/langchain/langchain/chat_models/openai.py index de4108e069d..7e1932165b2 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 assistant