From cec6296a168a49b1f39cfc2d92d576231dcf8e0e Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 17 Jul 2025 22:27:01 +0800 Subject: [PATCH 1/2] feat: add functionality to extract thought content from response To add the function of extracting thought content from the responses of thinking models (such as Gemini-2.5), developers need to obtain the thought text from the responses. --- google/genai/types.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/google/genai/types.py b/google/genai/types.py index 6dd364dc60..5d26c41351 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -5187,6 +5187,42 @@ def text(self) -> Optional[str]: """Returns the concatenation of all text parts in the response.""" return self._get_text(warn_property='text') + def _get_thoughts(self, warn_property: str = 'text') -> Optional[str]: + """Returns the concatenation of all thoughts parts in the response. + + This is an internal method that allows customizing the warning message. + """ + if ( + not self.candidates + or not self.candidates[0].content + or not self.candidates[0].content.parts + ): + return None + if len(self.candidates) > 1: + logger.warning( + f'there are {len(self.candidates)} candidates, returning' + f' {warn_property} result from the first candidate. Access' + ' response.candidates directly to get the result from other' + ' candidates.' + ) + text = '' + any_text_part_text = False + for part in self.candidates[0].content.parts: + if isinstance(part.text, str): + if isinstance(part.thought, bool) and part.thought: + any_text_part_text = True + text += part.text + + # part.text == '' is different from part.text is None + return text if any_text_part_text else None + + @property + def thoughts(self) -> Optional[str]: + """Returns the concatenation of all thoughts parts in the response.""" + return self._get_thoughts(warn_property='text') + + + @property def function_calls(self) -> Optional[list[FunctionCall]]: """Returns the list of function calls in the response.""" From 3c6598c94d736dc516767125144cc7a3971b0f5a Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 17 Jul 2025 23:07:57 +0800 Subject: [PATCH 2/2] style: adjust code formatting --- google/genai/types.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/google/genai/types.py b/google/genai/types.py index 5d26c41351..d31e97f977 100644 --- a/google/genai/types.py +++ b/google/genai/types.py @@ -5182,11 +5182,6 @@ def _get_text(self, warn_property: str = 'text') -> Optional[str]: # part.text == '' is different from part.text is None return text if any_text_part_text else None - @property - def text(self) -> Optional[str]: - """Returns the concatenation of all text parts in the response.""" - return self._get_text(warn_property='text') - def _get_thoughts(self, warn_property: str = 'text') -> Optional[str]: """Returns the concatenation of all thoughts parts in the response. @@ -5213,16 +5208,18 @@ def _get_thoughts(self, warn_property: str = 'text') -> Optional[str]: any_text_part_text = True text += part.text - # part.text == '' is different from part.text is None return text if any_text_part_text else None + @property + def text(self) -> Optional[str]: + """Returns the concatenation of all text parts in the response.""" + return self._get_text(warn_property='text') + @property def thoughts(self) -> Optional[str]: """Returns the concatenation of all thoughts parts in the response.""" return self._get_thoughts(warn_property='text') - - @property def function_calls(self) -> Optional[list[FunctionCall]]: """Returns the list of function calls in the response."""