def _preprocess_messages(self, messages: list[dict]) -> list[dict]:
        """
        Preprocess all messages to optimize images before sending to OpenAI API.
        """
        if not messages or not isinstance(messages, list):
            return messages

        processed_messages = []

        for msg in messages:
            # Skip system messages as they're handled separately
            if msg.get("role") == "system":
                processed_messages.append(msg)
                continue

            # Process array-format content (might contain images)
            if isinstance(msg.get("content"), list):
                new_msg = dict(msg)
                new_msg["content"] = self._process_array_content_with_images(
                    msg["content"]
                )
                processed_messages.append(new_msg)
            else:
                # Standard processing for non-array content
                processed_messages.append(msg)

        return processed_messages

        Preprocess all messages to optimize images before sending to OpenAI API.
        
py/core/providers/llm/openai.py