Function call and JSON ouput generate double responses

When I use a function and instruct with a JSON output format, most of the time (4/5), output JSON contains the answer x2 times:

Code:

def drive_to_poi(poi):
    print("drive_to_poi " + poi)
    return "True"

tools = [
    {
        "type": "function",
        "function": {
            "name": "drive_to_poi",
            "description": "Take me to a point of interest.",
            "parameters": {
                "type": "object",
                "properties": {
                    "poi": {
                        "type": "string",
                        "description": "Point of interest",
                    },
                },
                "required": ["poi"],
            },
        },
    }
]

messages = [
	{"role": "system", "content": "You are a tour guide. Output in JSON."},
	{"role": "user", "content": "Take me to Paris."},
]
response = client.chat.completions.create(
    model="gpt-3.5-turbo-1106",
    messages=messages,
    response_format={"type": "json_object"},
    tools=tools,
    tool_choice="auto",
)

response_message = response.choices[0].message
tool_calls = response_message.tool_calls
if tool_calls:
    available_functions = {
        "drive_to_poi": drive_to_poi,
    }
    for tool_call in tool_calls:
        function_name = tool_call.function.name
        function_to_call = available_functions[function_name]
        function_args = json.loads(tool_call.function.arguments)
        function_response = function_to_call(
            poi=function_args.get("poi"),
        )

        message=(
            {
                "role": response_message.role,
                "function_call": {
                    "name": function_name,
                    "arguments": tool_call.function.arguments,
                },
                "content": None
            }
        )
        messages.append(message)
            
        message=(
            {
                "tool_call_id": tool_call.id,
                "role": "function",
                "name": function_name,
                "content": function_response,
            }
        )
        messages.append(message)
        print('\nMessages:')
        pprint.pprint(messages)
    
    second_response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=messages,
        response_format={"type": "json_object"},
        tools=tools,
        tool_choice="auto",
    )
    print('\nSecond_response:')
    pprint.pprint(second_response)

Right JSON ouput:

Messages:
[{'content': 'You are a tour guide. Output in JSON.', 'role': 'system'},
 {'content': 'Take me to Paris.', 'role': 'user'},
 {'content': None,
  'function_call': {'arguments': '{"poi":"Paris"}', 'name': 'drive_to_poi'},
  'role': 'assistant'},
 {'content': 'True',
  'name': 'drive_to_poi',
  'role': 'function',
  'tool_call_id': 'call_XXXXXX'}]

Second_response:
ChatCompletion(id='chatcmpl-8lJjWyktMO3E1J0TWTfMH0rzDO1Tm', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='
{\n  "status": "success",\n  "message": "You have arrived in Paris. Enjoy your time in the City of Light!"\n}
', role='assistant', function_call=None, tool_calls=None), logprobs=None)], created=1706287962, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint='fp_b57c83dd65', usage=CompletionUsage(completion_tokens=30, prompt_tokens=96, total_tokens=126))

Wrong JSON Ouput:

Messages:
[{'content': 'You are a tour guide. Output in JSON.', 'role': 'system'},
 {'content': 'Take me to Paris.', 'role': 'user'},
 {'content': None,
  'function_call': {'arguments': '{"poi":"Paris"}', 'name': 'drive_to_poi'},
  'role': 'assistant'},
 {'content': 'True',
  'name': 'drive_to_poi',
  'role': 'function',
  'tool_call_id': 'call_XXXXXX'}]

Second_response:
ChatCompletion(id='chatcmpl-8lJX5ccTgIwXGHUmWT6wCQeCFBZns', choices=[Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content='
{\n "response": "You have arrived in Paris. Welcome to the City of Light!"\n}\n
{\n "response": "You have arrived in Paris. Welcome to the City of Light!"\n}',
 role='assistant', function_call=None, tool_calls=None), logprobs=None)], created=1706287191, model='gpt-3.5-turbo-1106', object='chat.completion', system_fingerprint='fp_b57c83dd65', usage=CompletionUsage(completion_tokens=42, prompt_tokens=96, total_tokens=138))

When disabling function or JSON ouput, no issue.

Am i missing something around function feedback and messages management?

hey @manu3b . It seems like we are having the same issue now. Have you found the root cause?

Hi @janm , yes, it appears that the issue disappears if I disable functions during the function result response:

    second_response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=messages,
        response_format={"type": "json_object"},
#        tools=tools,
#        tool_choice="auto",
    )

That’s quite weird as this implies to not be able to generate another function call following this 1rst return.

I had the same. If happens randomly, sometimes even on the first call for me.