Created
March 16, 2025 01:56
-
-
Save jezell/20436285a343e9ce1d6fc4fe0e7481d4 to your computer and use it in GitHub Desktop.
openai_parallel_tool_call_id_bug.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openai import AsyncOpenAI | |
import os | |
import asyncio | |
openai=AsyncOpenAI( | |
api_key=os.getenv("OPENAI_API_KEY") | |
) | |
async def run(): | |
previous_response_id = None | |
messages = [ | |
{ "role" : "user", "content" : "what's the weather like in sf for each type of unit" } | |
] | |
for i in range(10): | |
response = await openai.responses.create( | |
model="gpt-4o", | |
stream=True, | |
input=messages, | |
parallel_tool_calls=True, | |
previous_response_id=previous_response_id, | |
tools=[ | |
{ | |
"type": "function", | |
"name": "lookupLocationId", | |
"strict" : True, | |
"parameters": { | |
"additionalProperties" : False, | |
"required" : [ "name" ], | |
"type": "object", | |
"properties": { | |
"name": { | |
"type": "string", | |
"description": "The location to to lookup the id for.", | |
}, | |
} | |
} | |
}, | |
{ | |
"type": "function", | |
"name": "lookupUnits", | |
"strict" : True, | |
"parameters": { | |
"additionalProperties" : False, | |
"required" : [], | |
"type": "object", | |
"properties": { | |
} | |
} | |
}, | |
{ | |
"type": "function", | |
"name": "getWeather", | |
"strict" : True, | |
"parameters": { | |
"additionalProperties" : False, | |
"required" : [ "location_id", "unit"], | |
"type": "object", | |
"properties": { | |
"location_id": { | |
"type": "string", | |
"description": "The location to get the weather for.", | |
}, | |
"unit": { | |
"type": "string", | |
"description": "The unit of measurement to use.", | |
} | |
} | |
} | |
} | |
], | |
) | |
async for event in response: | |
if event.type == "response.completed": | |
previous_response_id = event.response.id | |
print("----") | |
for output in event.response.output: | |
if output.type == "function_call": | |
print("----> "+output.call_id) | |
elif event.type == "response.output_item.done": | |
if event.item.type == "function_call": | |
print(event.item) | |
if event.item.name == "lookupLocationId": | |
messages.append({ | |
"output" : "1234", | |
"call_id" : event.item.call_id, | |
"type" : "function_call_output" | |
}) | |
elif event.item.name == "lookupUnits": | |
messages.append({ | |
"output" : '{"units":["C","F"]}', | |
"call_id" : event.item.call_id, | |
"type" : "function_call_output" | |
}) | |
else: | |
messages.append({ | |
"output" : "error: did not get the weather, try again", | |
"call_id" : event.item.call_id, | |
"type" : "function_call_output" | |
}) | |
if __name__ == '__main__': | |
loop = asyncio.new_event_loop() | |
asyncio.set_event_loop(loop) | |
asyncio.get_event_loop().run_until_complete(run()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment