Expects tool calls when none are specified

I’m experiencing a recurring error with the GPT-4 API call. The error message reads as follows:

Error during Vision GPT-4 API call: Error code: 400 - {‘error’: {‘message’: “Invalid ‘messages[2].tool_calls’: empty array. Expected an array with minimum length 1, but got an empty array instead.”, ‘type’: ‘invalid_request_error’, ‘param’: ‘messages[2].tool_calls’, ‘code’: ‘empty_array’}}

Here’s the code where the error occurs. I suspect it might be related to how the conversation history is structured, but I’m not entirely sure:

from openai import OpenAI
from settings import SYSTEM_PROMPT
import logging
from envvar import OPENAI_API_KEY
from actions.assets.actions_models import Actions
import sys
from vision.assets.actions_models import VisionInterpretation


class ChatGPTSession:
    def __init__(self) -> None:
        self.conversation_history = [
            {"role": "system", "content": SYSTEM_PROMPT},
        ]
        self.client = OpenAI(api_key=OPENAI_API_KEY)

    async def get__actions(self, transcription: str):
        self.conversation_history.append(
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": transcription,
                    }
                ],
            }
        )

        # Send the transcription to gpt-4o
        try:
            # Make the OpenAI GPT-4 API call with the full conversation history
            response = self.client.beta.chat.completions.parse(
                model="gpt-4o",
                messages=self.conversation_history,
                max_tokens=200,
                response_format=Actions,
                n=1,
            )

        except Exception as e:
            logging.error(f"Error during Actions GPT-4 API call: {e}")
            sys.exit(1)

        response_message = response.choices[0].message
        _actions: Actions = response_message.parsed
        self.conversation_history.append(response_message)

        # Return  Actions
        return _actions

    async def give_vision(self, b64_video_segment_grid):
        self.conversation_history.append(
            {
                "role": "user",
                "content": [
                    {
                        "type": "text",
                        "text": "Here is what you see",
                    },
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/jpeg;base64,{b64_video_segment_grid}",
                            "detail": "low",
                        },
                    },
                ],
            },
        )

        logging.info("sending vision grid")
        # Send the vision grid to gpt-4o
        try:
            # Make the OpenAI GPT-4 API call with the full conversation history
            response = self.client.beta.chat.completions.parse(
                model="gpt-4o",
                messages=self.conversation_history,
                max_tokens=200,
                response_format=VisionInterpretation,
                n=1,
            )

        except Exception as e:
            logging.error(f"Error during Vision GPT-4 API call: {e}")
            sys.exit(1)
        pass

        response_message = response.choices[0].message
        vision_interpr: VisionInterpretation = response_message.parsed
        self.conversation_history.append(response_message)
        return vision_interpr.interpreted_vision

It appears the error relates to messages[2].tool_calls having an empty array, but I’m unsure why. If anyone has encountered this or has insights on ensuring a valid tool_calls array, I’d appreciate the help!

Fixed it, had to call del response_message.tool_calls before appending to conversation history