Skip to content

Commit e1aff00

Browse files
authored
groq: support reasoning_effort, update docs for clarity (#31754)
- There was some ambiguous wording that has been updated to hopefully clarify the functionality of `reasoning_format` in ChatGroq. - Added support for `reasoning_effort` - Added links to see models capable of `reasoning_format` and `reasoning_effort` - Other minor nits
1 parent ea1345a commit e1aff00

File tree

6 files changed

+72
-32
lines changed

6 files changed

+72
-32
lines changed

β€Žlibs/partners/ai21/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

β€Žlibs/partners/groq/langchain_groq/chat_models.py

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383

8484

8585
class ChatGroq(BaseChatModel):
86-
"""`Groq` Chat large language models API.
86+
"""Groq Chat large language models API.
8787
8888
To use, you should have the
8989
environment variable ``GROQ_API_KEY`` set with your API key.
@@ -102,17 +102,27 @@ class ChatGroq(BaseChatModel):
102102
103103
Key init args β€” completion params:
104104
model: str
105-
Name of Groq model to use. E.g. "llama-3.1-8b-instant".
105+
Name of Groq model to use, e.g. ``llama-3.1-8b-instant``.
106106
temperature: float
107-
Sampling temperature. Ranges from 0.0 to 1.0.
107+
Sampling temperature. Ranges from ``0.0`` to ``1.0``.
108108
max_tokens: Optional[int]
109109
Max number of tokens to generate.
110110
reasoning_format: Optional[Literal["parsed", "raw", "hidden]]
111-
The format for reasoning output.
112-
113-
- ``parsed``: Separates reasoning into a dedicated field while keeping the response concise.
114-
- ``raw``: Includes reasoning within think tags in the content.
115-
- ``hidden``: Returns only the final answer.
111+
The format for reasoning output. Groq will default to ``raw`` if left
112+
undefined.
113+
114+
- ``'parsed'``: Separates reasoning into a dedicated field while keeping the
115+
response concise. Reasoning will be returned in the
116+
``additional_kwargs.reasoning_content`` field of the response.
117+
- ``'raw'``: Includes reasoning within think tags (e.g.
118+
``<think>{reasoning_content}</think>``).
119+
- ``'hidden'``: Returns only the final answer content. Note: this only
120+
supresses reasoning content in the response; the model will still perform
121+
reasoning unless overridden in ``reasoning_effort``.
122+
123+
See the `Groq documentation
124+
<https://console.groq.com/docs/reasoning#reasoning>`__ for more
125+
details and a list of supported reasoning models.
116126
model_kwargs: Dict[str, Any]
117127
Holds any model parameters valid for create call not
118128
explicitly specified.
@@ -123,7 +133,7 @@ class ChatGroq(BaseChatModel):
123133
max_retries: int
124134
Max number of retries.
125135
api_key: Optional[str]
126-
Groq API key. If not passed in will be read from env var GROQ_API_KEY.
136+
Groq API key. If not passed in will be read from env var ``GROQ_API_KEY``.
127137
base_url: Optional[str]
128138
Base URL path for API requests, leave blank if not using a proxy
129139
or service emulator.
@@ -168,11 +178,9 @@ class ChatGroq(BaseChatModel):
168178
'logprobs': None}, id='run-ecc71d70-e10c-4b69-8b8c-b8027d95d4b8-0')
169179
170180
Stream:
171-
172-
Streaming `text` for each content chunk received:
173-
174181
.. code-block:: python
175182
183+
# Streaming `text` for each content chunk received
176184
for chunk in llm.stream(messages):
177185
print(chunk.text(), end="")
178186
@@ -188,10 +196,9 @@ class ChatGroq(BaseChatModel):
188196
content='' response_metadata={'finish_reason': 'stop'}
189197
id='run-4e9f926b-73f5-483b-8ef5-09533d925853
190198
191-
Reconstructing a full response:
192-
193199
.. code-block:: python
194200
201+
# Reconstructing a full response
195202
stream = llm.stream(messages)
196203
full = next(stream)
197204
for chunk in stream:
@@ -283,7 +290,7 @@ class Joke(BaseModel):
283290
284291
See ``ChatGroq.with_structured_output()`` for more.
285292
286-
Response metadata
293+
Response metadata:
287294
.. code-block:: python
288295
289296
ai_msg = llm.invoke(messages)
@@ -302,7 +309,7 @@ class Joke(BaseModel):
302309
'system_fingerprint': 'fp_c5f20b5bb1',
303310
'finish_reason': 'stop',
304311
'logprobs': None}
305-
""" # noqa: E501
312+
"""
306313

307314
client: Any = Field(default=None, exclude=True) #: :meta private:
308315
async_client: Any = Field(default=None, exclude=True) #: :meta private:
@@ -312,23 +319,44 @@ class Joke(BaseModel):
312319
"""What sampling temperature to use."""
313320
stop: Optional[Union[list[str], str]] = Field(default=None, alias="stop_sequences")
314321
"""Default stop sequences."""
315-
reasoning_format: Optional[Literal["parsed", "raw", "hidden"]] = None
316-
"""The format for reasoning output.
317-
318-
- ``parsed``: Separates reasoning into a dedicated field while keeping the response concise.
319-
- ``raw``: Includes reasoning within think tags in the content.
320-
- ``hidden``: Returns only the final answer.
321-
""" # noqa: E501
322+
reasoning_format: Optional[Literal["parsed", "raw", "hidden"]] = Field(default=None)
323+
"""The format for reasoning output. Groq will default to raw if left undefined.
324+
325+
- ``'parsed'``: Separates reasoning into a dedicated field while keeping the
326+
response concise. Reasoning will be returned in the
327+
``additional_kwargs.reasoning_content`` field of the response.
328+
- ``'raw'``: Includes reasoning within think tags (e.g.
329+
``<think>{reasoning_content}</think>``).
330+
- ``'hidden'``: Returns only the final answer content. Note: this only supresses
331+
reasoning content in the response; the model will still perform reasoning unless
332+
overridden in ``reasoning_effort``.
333+
334+
See the `Groq documentation <https://console.groq.com/docs/reasoning#reasoning>`__
335+
for more details and a list of supported reasoning models.
336+
"""
337+
reasoning_effort: Optional[Literal["none", "default"]] = Field(default=None)
338+
"""The level of effort the model will put into reasoning. Groq will default to
339+
enabling reasoning if left undefined. If set to ``none``, ``reasoning_format`` will
340+
not apply and ``reasoning_content`` will not be returned.
341+
342+
- ``'none'``: Disable reasoning. The model will not use any reasoning tokens when
343+
generating a response.
344+
- ``'default'``: Enable reasoning.
345+
346+
See the `Groq documentation
347+
<https://console.groq.com/docs/reasoning#options-for-reasoning-effort>`__ for more
348+
details and a list of models that support setting a reasoning effort.
349+
"""
322350
model_kwargs: dict[str, Any] = Field(default_factory=dict)
323351
"""Holds any model parameters valid for `create` call not explicitly specified."""
324352
groq_api_key: Optional[SecretStr] = Field(
325353
alias="api_key", default_factory=secret_from_env("GROQ_API_KEY", default=None)
326354
)
327-
"""Automatically inferred from env var `GROQ_API_KEY` if not provided."""
355+
"""Automatically inferred from env var ``GROQ_API_KEY`` if not provided."""
328356
groq_api_base: Optional[str] = Field(
329357
alias="base_url", default_factory=from_env("GROQ_API_BASE", default=None)
330358
)
331-
"""Base URL path for API requests, leave blank if not using a proxy or service
359+
"""Base URL path for API requests. Leave blank if not using a proxy or service
332360
emulator."""
333361
# to support explicit proxy for Groq
334362
groq_proxy: Optional[str] = Field(
@@ -426,11 +454,11 @@ def validate_environment(self) -> Self:
426454
self.async_client = groq.AsyncGroq(
427455
**client_params, **async_specific
428456
).chat.completions
429-
except ImportError:
457+
except ImportError as exc:
430458
raise ImportError(
431459
"Could not import groq python package. "
432460
"Please install it with `pip install groq`."
433-
)
461+
) from exc
434462
return self
435463

436464
#
@@ -624,6 +652,7 @@ def _default_params(self) -> dict[str, Any]:
624652
"temperature": self.temperature,
625653
"stop": self.stop,
626654
"reasoning_format": self.reasoning_format,
655+
"reasoning_effort": self.reasoning_effort,
627656
**self.model_kwargs,
628657
}
629658
if self.max_tokens is not None:
@@ -1227,7 +1256,7 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
12271256
for raw_tool_call in raw_tool_calls:
12281257
try:
12291258
tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))
1230-
except Exception as e:
1259+
except Exception as e: # pylint: disable=broad-except
12311260
invalid_tool_calls.append(
12321261
make_invalid_tool_call(raw_tool_call, str(e))
12331262
)

β€Žlibs/partners/groq/tests/integration_tests/test_chat_models.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ def test_reasoning_output_stream() -> None:
264264
assert len(full_response.additional_kwargs["reasoning_content"]) > 0
265265

266266

267+
def test_reasoning_effort_none() -> None:
268+
"""Test that no reasoning output is returned if effort is set to none."""
269+
chat = ChatGroq(
270+
model="qwen/qwen3-32b", # Only qwen3 currently supports reasoning_effort
271+
reasoning_effort="none",
272+
)
273+
message = HumanMessage(content="What is the capital of France?")
274+
response = chat.invoke([message])
275+
assert isinstance(response, AIMessage)
276+
assert "reasoning_content" not in response.additional_kwargs
277+
assert "<think>" not in response.content and "<think/>" not in response.content
278+
279+
267280
#
268281
# Misc tests
269282
#

β€Žlibs/partners/ollama/langchain_ollama/chat_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class Multiply(BaseModel):
450450
"""
451451

452452
sync_client_kwargs: Optional[dict] = {}
453-
"""Additional kwargs to merge with client_kwargs before passing to the httpx Client.
453+
"""Additional kwargs to merge with client_kwargs before passing to the HTTPX Client.
454454
455455
For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#client>`__.
456456
"""

β€Žlibs/partners/ollama/langchain_ollama/embeddings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class OllamaEmbeddings(BaseModel, Embeddings):
141141
"""
142142

143143
sync_client_kwargs: Optional[dict] = {}
144-
"""Additional kwargs to merge with client_kwargs before passing to the httpx Client.
144+
"""Additional kwargs to merge with client_kwargs before passing to the HTTPX Client.
145145
146146
For a full list of the params, see the `HTTPX documentation <https://www.python-httpx.org/api/#client>`__.
147147
"""

β€Žlibs/partners/pinecone/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
Β (0)