Repository: mem0ai/mem0
File: mem0/llms/gemini.py (Python)

Code before:
            self.config.model = "gemini-1.5-flash-latest"

        api_key = self.config.api_key or os.getenv("GEMINI_API_KEY")
        genai.configure(api_key=api_key)
        self.client = GenerativeModel(model_name=self.config.model)

    def _parse_response(self, response, tools):
        """
        Process the response based on whether tools are used or not.

        Args:
            response: The raw response from API.
            tools: The list of tools provided in the request.

        Returns:
            str or dict: The processed response.
        """
        if tools:
            processed_response = {
                "content": content if (content := response.candidates[0].content.parts[0].text) else None,
                "tool_calls": [],
            }

            for part in response.candidates[0].content.parts:
                if fn := part.function_call:
                    fn_call = type(fn).to_dict(fn)
                    processed_response["tool_calls"].append(
                        {"name": fn_call["name"], "arguments": fn_call["args"]}
                    )

            return processed_response
        else:
            return response.candidates[0].content.parts[0].text

    def _reformat_messages(self, messages: List[Dict[str, str]]):
        """
        Reformat messages for Gemini.

        Args:
            messages: The list of messages provided in the request.

        Returns:
            list: The list of messages in the required format.
        """
        new_messages = []

        for message in messages:
            if message["role"] == "system":
                content = "THIS IS A SYSTEM PROMPT. YOU MUST OBEY THIS: " + message["content"]

            else:

Review comment (refactor):
The old syntax doesn't work properly with some cases of functions with deep values ​​or iterations. The update is referenced this way: https://ai.google.dev/gemini-api/tutorials/extract_structured_data

Code after:
            self.config.model = "gemini-1.5-flash-latest"

        api_key = self.config.api_key or os.getenv("GEMINI_API_KEY")
        genai.configure(api_key=api_key)
        self.client = GenerativeModel(model_name=self.config.model)

    def _parse_response(self, response, tools):
        """
        Process the response based on whether tools are used or not.

        Args:
            response: The raw response from API.
            tools: The list of tools provided in the request.

        Returns:
            str or dict: The processed response.
        """
        if tools:
            processed_response = {
                "content": (
                    content
                    if (content := response.candidates[0].content.parts[0].text)
                    else None
                ),
                "tool_calls": [],
            }

            for part in response.candidates[0].content.parts:
                if fn := part.function_call:
                    if isinstance(fn, protos.FunctionCall):
                        fn_call = type(fn).to_dict(fn)
                        processed_response["tool_calls"].append(
                            {"name": fn_call["name"], "arguments": fn_call["args"]}
                        )
                        continue
                    processed_response["tool_calls"].append(
                        {"name": fn.name, "arguments": fn.args}
                    )

            return processed_response
        else:
            return response.candidates[0].content.parts[0].text

    def _reformat_messages(self, messages: List[Dict[str, str]]):
        """
        Reformat messages for Gemini.

        Args:
            messages: The list of messages provided in the request.

        Returns: