diff --git a/lib/crewai/src/crewai/events/types/llm_events.py b/lib/crewai/src/crewai/events/types/llm_events.py index 87087f100..73d743804 100644 --- a/lib/crewai/src/crewai/events/types/llm_events.py +++ b/lib/crewai/src/crewai/events/types/llm_events.py @@ -86,3 +86,11 @@ class LLMStreamChunkEvent(LLMEventBase): tool_call: ToolCall | None = None call_type: LLMCallType | None = None response_id: str | None = None + + +class LLMThinkingChunkEvent(LLMEventBase): + """Event emitted when a thinking/reasoning chunk is received from a thinking model""" + + type: str = "llm_thinking_chunk" + chunk: str + response_id: str | None = None diff --git a/lib/crewai/src/crewai/llms/base_llm.py b/lib/crewai/src/crewai/llms/base_llm.py index dcb261fd7..67222f088 100644 --- a/lib/crewai/src/crewai/llms/base_llm.py +++ b/lib/crewai/src/crewai/llms/base_llm.py @@ -26,6 +26,7 @@ from crewai.events.types.llm_events import ( LLMCallStartedEvent, LLMCallType, LLMStreamChunkEvent, + LLMThinkingChunkEvent, ) from crewai.events.types.tool_usage_events import ( ToolUsageErrorEvent, @@ -465,6 +466,35 @@ class BaseLLM(ABC): ), ) + def _emit_thinking_chunk_event( + self, + chunk: str, + from_task: Task | None = None, + from_agent: Agent | None = None, + response_id: str | None = None, + ) -> None: + """Emit thinking/reasoning chunk event from a thinking model. + + Args: + chunk: The thinking text content. + from_task: The task that initiated the call. + from_agent: The agent that initiated the call. + response_id: Unique ID for a particular LLM response. + """ + if not hasattr(crewai_event_bus, "emit"): + raise ValueError("crewai_event_bus does not have an emit method") from None + + crewai_event_bus.emit( + self, + event=LLMThinkingChunkEvent( + chunk=chunk, + from_task=from_task, + from_agent=from_agent, + response_id=response_id, + call_id=get_current_call_id(), + ), + ) + def _handle_tool_execution( self, function_name: str, diff --git a/lib/crewai/src/crewai/llms/providers/gemini/completion.py b/lib/crewai/src/crewai/llms/providers/gemini/completion.py index bd634c8dc..d854c150d 100644 --- a/lib/crewai/src/crewai/llms/providers/gemini/completion.py +++ b/lib/crewai/src/crewai/llms/providers/gemini/completion.py @@ -61,6 +61,7 @@ class GeminiCompletion(BaseLLM): interceptor: BaseInterceptor[Any, Any] | None = None, use_vertexai: bool | None = None, response_format: type[BaseModel] | None = None, + thinking_config: types.ThinkingConfig | None = None, **kwargs: Any, ): """Initialize Google Gemini chat completion client. @@ -93,6 +94,10 @@ class GeminiCompletion(BaseLLM): api_version="v1" is automatically configured. response_format: Pydantic model for structured output. Used as default when response_model is not passed to call()/acall() methods. + thinking_config: ThinkingConfig for thinking models (gemini-2.5+, gemini-3+). + Controls thought output via include_thoughts, thinking_budget, + and thinking_level. When None, thinking models automatically + get include_thoughts=True so thought content is surfaced. **kwargs: Additional parameters """ if interceptor is not None: @@ -139,6 +144,14 @@ class GeminiCompletion(BaseLLM): version_match and float(version_match.group(1)) >= 2.0 ) + self.thinking_config = thinking_config + if ( + self.thinking_config is None + and version_match + and float(version_match.group(1)) >= 2.5 + ): + self.thinking_config = types.ThinkingConfig(include_thoughts=True) + @property def stop(self) -> list[str]: """Get stop sequences sent to the API.""" @@ -520,6 +533,9 @@ class GeminiCompletion(BaseLLM): if self.safety_settings: config_params["safety_settings"] = self.safety_settings + if self.thinking_config is not None: + config_params["thinking_config"] = self.thinking_config + return types.GenerateContentConfig(**config_params) def _convert_tools_for_interference( # type: ignore[override] @@ -931,15 +947,6 @@ class GeminiCompletion(BaseLLM): if chunk.usage_metadata: usage_data = self._extract_token_usage(chunk) - if chunk.text: - full_response += chunk.text - self._emit_stream_chunk_event( - chunk=chunk.text, - from_task=from_task, - from_agent=from_agent, - response_id=response_id, - ) - if chunk.candidates: candidate = chunk.candidates[0] if candidate.content and candidate.content.parts: @@ -976,6 +983,21 @@ class GeminiCompletion(BaseLLM): call_type=LLMCallType.TOOL_CALL, response_id=response_id, ) + elif part.thought and part.text: + self._emit_thinking_chunk_event( + chunk=part.text, + from_task=from_task, + from_agent=from_agent, + response_id=response_id, + ) + elif part.text: + full_response += part.text + self._emit_stream_chunk_event( + chunk=part.text, + from_task=from_task, + from_agent=from_agent, + response_id=response_id, + ) return full_response, function_calls, usage_data @@ -1329,7 +1351,7 @@ class GeminiCompletion(BaseLLM): text_parts = [ part.text for part in candidate.content.parts - if hasattr(part, "text") and part.text + if part.text and not part.thought ] return "".join(text_parts) diff --git a/uv.lock b/uv.lock index 55db75b2c..a1850ebe9 100644 --- a/uv.lock +++ b/uv.lock @@ -1197,7 +1197,7 @@ requires-dist = [ { name = "crewai-files", marker = "extra == 'file-processing'", editable = "lib/crewai-files" }, { name = "crewai-tools", marker = "extra == 'tools'", editable = "lib/crewai-tools" }, { name = "docling", marker = "extra == 'docling'", specifier = "~=2.75.0" }, - { name = "google-genai", marker = "extra == 'google-genai'", specifier = "~=1.49.0" }, + { name = "google-genai", marker = "extra == 'google-genai'", specifier = "~=1.65.0" }, { name = "httpx", specifier = "~=0.28.1" }, { name = "httpx-auth", marker = "extra == 'a2a'", specifier = "~=0.23.1" }, { name = "httpx-sse", marker = "extra == 'a2a'", specifier = "~=0.4.0" }, @@ -2249,6 +2249,11 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/83/1d/d6466de3a5249d35e832a52834115ca9d1d0de6abc22065f049707516d47/google_auth-2.48.0-py3-none-any.whl", hash = "sha256:2e2a537873d449434252a9632c28bfc268b0adb1e53f9fb62afc5333a975903f", size = 236499, upload-time = "2026-01-26T19:22:45.099Z" }, ] +[package.optional-dependencies] +requests = [ + { name = "requests" }, +] + [[package]] name = "google-cloud-vision" version = "3.12.1" @@ -2267,21 +2272,23 @@ wheels = [ [[package]] name = "google-genai" -version = "1.49.0" +version = "1.65.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "anyio" }, - { name = "google-auth" }, + { name = "distro" }, + { name = "google-auth", extra = ["requests"] }, { name = "httpx" }, { name = "pydantic" }, { name = "requests" }, + { name = "sniffio" }, { name = "tenacity" }, { name = "typing-extensions" }, { name = "websockets" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/82/49/1a724ee3c3748fa50721d53a52d9fee88c67d0c43bb16eb2b10ee89ab239/google_genai-1.49.0.tar.gz", hash = "sha256:35eb16023b72e298571ae30e919c810694f258f2ba68fc77a2185c7c8829ad5a", size = 253493, upload-time = "2025-11-05T22:41:03.278Z" } +sdist = { url = "https://files.pythonhosted.org/packages/79/f9/cc1191c2540d6a4e24609a586c4ed45d2db57cfef47931c139ee70e5874a/google_genai-1.65.0.tar.gz", hash = "sha256:d470eb600af802d58a79c7f13342d9ea0d05d965007cae8f76c7adff3d7a4750", size = 497206, upload-time = "2026-02-26T00:20:33.824Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/d5/d3/84a152746dc7bdebb8ba0fd7d6157263044acd1d14b2a53e8df4a307b6b7/google_genai-1.49.0-py3-none-any.whl", hash = "sha256:ad49cd5be5b63397069e7aef9a4fe0a84cbdf25fcd93408e795292308db4ef32", size = 256098, upload-time = "2025-11-05T22:41:01.429Z" }, + { url = "https://files.pythonhosted.org/packages/68/3c/3fea4e7c91357c71782d7dcaad7a2577d636c90317e003386893c25bc62c/google_genai-1.65.0-py3-none-any.whl", hash = "sha256:68c025205856919bc03edb0155c11b4b833810b7ce17ad4b7a9eeba5158f6c44", size = 724429, upload-time = "2026-02-26T00:20:32.186Z" }, ] [[package]]