Skip to content

The agent engineering platform.

fix(groq): handle content correctly (#33363)

Mason Daugherty68c56440cfb9
diff --git a/libs/partners/groq/langchain_groq/chat_models.py b/libs/partners/groq/langchain_groq/chat_models.py
index 0fa0770289..4567b41eba 100644
--- a/libs/partners/groq/langchain_groq/chat_models.py
+++ b/libs/partners/groq/langchain_groq/chat_models.py
@@ -70,7 +70,7 @@ class ChatGroq(BaseChatModel):
can be passed in, even if not explicitly saved on this class.
Setup:
- Install ``langchain-groq`` and set environment variable
+ Install `langchain-groq` and set environment variable
``GROQ_API_KEY``.
.. code-block:: bash
@@ -1199,6 +1199,17 @@ def _convert_message_to_dict(message: BaseMessage) -> dict:
message_dict = {"role": "user", "content": message.content}
elif isinstance(message, AIMessage):
message_dict = {"role": "assistant", "content": message.content}
+
+ # If content is a list of content blocks, filter out tool_call blocks
+ # as Groq API only accepts 'text' type blocks in content
+ if isinstance(message.content, list):
+ text_blocks = [
+ block
+ for block in message.content
+ if isinstance(block, dict) and block.get("type") == "text"
+ ]
+ message_dict["content"] = text_blocks if text_blocks else ""
+
if "function_call" in message.additional_kwargs:
message_dict["function_call"] = message.additional_kwargs["function_call"]
# If function call only, content is None not empty string
@@ -1211,10 +1222,19 @@ def _convert_message_to_dict(message: BaseMessage) -> dict:
_lc_invalid_tool_call_to_groq_tool_call(tc)
for tc in message.invalid_tool_calls
]
+ # If tool calls only (no text blocks), content is None not empty string
+ if message_dict["content"] == "" or (
+ isinstance(message_dict["content"], list)
+ and not message_dict["content"]
+ ):
+ message_dict["content"] = None
elif "tool_calls" in message.additional_kwargs:
message_dict["tool_calls"] = message.additional_kwargs["tool_calls"]
# If tool calls only, content is None not empty string
- if message_dict["content"] == "":
+ if message_dict["content"] == "" or (
+ isinstance(message_dict["content"], list)
+ and not message_dict["content"]
+ ):
message_dict["content"] = None
elif isinstance(message, SystemMessage):
message_dict = {"role": "system", "content": message.content}
diff --git a/libs/partners/groq/uv.lock b/libs/partners/groq/uv.lock
index b96919eb44..7ba3922488 100644
--- a/libs/partners/groq/uv.lock
+++ b/libs/partners/groq/uv.lock
@@ -212,7 +212,7 @@ name = "exceptiongroup"
version = "1.3.0"
source = { registry = "https://pypi.org/simple" }
dependencies = [
- { name = "typing-extensions", marker = "python_full_version < '3.13'" },
+ { name = "typing-extensions", marker = "python_full_version < '3.11'" },
]
sdist = { url = "https://files.pythonhosted.org/packages/0b/9f/a65090624ecf468cdca03533906e7c69ed7588582240cfe7cc9e770b50eb/exceptiongroup-1.3.0.tar.gz", hash = "sha256:b241f5885f560bc56a59ee63ca4c6a8bfa46ae4ad651af316d4e81817bb9fd88", size = 29749, upload-time = "2025-05-10T17:42:51.123Z" }
wheels = [
@@ -314,7 +314,7 @@ wheels = [
[[package]]
name = "langchain-core"
-version = "1.0.0a6"
+version = "1.0.0a8"
source = { editable = "../../core" }
dependencies = [
{ name = "jsonpatch" },