Skip to content

Commit d55ac7f

Browse files
[BUGS] Prevent NullPointerException for Gemini models with empty AIMessage content (#79)
* Fix NullPointerException for empty message content Adds a check for empty message content before processing in GenericProvider, returning a default text when content is missing. * Fix tool_call id assignment in ToolCall creation Refactors the logic for assigning the id field when creating a ToolCall object to ensure an id is always set, generating a new UUID if necessary. * Make format to appease the lint god
1 parent 17be3c4 commit d55ac7f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

libs/oci/langchain_oci/chat_models/oci_generative_ai.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,17 @@ def convert_oci_tool_call_to_langchain(tool_call: Any) -> ToolCall:
110110
# If it's not valid JSON, keep it as a string
111111
pass
112112

113+
if "id" in tool_call.attribute_map and tool_call.id:
114+
id = tool_call.id
115+
else:
116+
id = uuid.uuid4().hex
117+
113118
return ToolCall(
114119
name=tool_call.name,
115120
args=parsed
116121
if "arguments" in tool_call.attribute_map
117122
else tool_call.parameters,
118-
id=tool_call.id if "id" in tool_call.attribute_map else uuid.uuid4().hex[:],
123+
id=id,
119124
)
120125

121126
@staticmethod
@@ -815,7 +820,12 @@ def messages_to_oci_params(
815820
message.tool_calls or message.additional_kwargs.get("tool_calls")
816821
):
817822
# Process content and tool calls for assistant messages
818-
content = self._process_message_content(message.content)
823+
if message.content:
824+
content = self._process_message_content(message.content)
825+
# Issue 78 fix: Check if original content is empty BEFORE processing
826+
# to prevent NullPointerException in OCI backend
827+
else:
828+
content = [self.oci_chat_message_text_content(text=".")]
819829
tool_calls = []
820830
for tool_call in message.tool_calls:
821831
tool_calls.append(

0 commit comments

Comments
 (0)