Hello,
I have a problem ini my python implementation with openai SDK using the responses API with stream and function calling.
It´s all ok, the endpoint works fine streaming text messages and file search.
I’m implementing the function calling with stream, and i receive ok the model request, i excecute my local function, but i don’t know how can i send to the model the function output…
In the Assistants API there is a submit_tool_outputs fucntion, but i don´t find the way to do this with the Responses API.
This is my code
for event in response:
event_type = event.type
print(f"[DEBUG] event_type recibido: {event_type}")
if event_type == "response.created" and not current_response_id:
current_response_id = event.response.id
response_data = {
"response_id": current_response_id,
"conversation_id": conversation_id,
"previous_response_id": previous_response_id if previous_response_id else None,
"created_at": datetime.datetime.fromtimestamp(event.response.created_at),
"model": event.response.model,
"input": input_messages # Guardar los mensajes completos, incluyendo el de sistema
}
yield f"event: current_response_id\ndata: {json.dumps({'response_id': current_response_id, 'conversation_id': conversation_id})}\n\n"
if event_type == "response.completed":
response_data.update({
"output": event.response.output,
"total_tokens": event.response.usage.total_tokens,
"input_tokens": event.response.usage.input_tokens,
"output_tokens": event.response.usage.output_tokens,
"response_object": json.loads(event.model_dump_json())
})
await save_response_to_db(response_data, db)
if event_type == "response.output_item.added":
# Aquí suele venir el nombre de la función
if hasattr(event, "output_item") and event.output_item.type == "function_call":
function_name = event.output_item.name
item_id = event.output_item.id
pending_function_calls[item_id] = function_name
if event_type == "response.function_call_arguments.done":
function_args = json.loads(event.arguments)
function_call_id = event.item_id
function_name = pending_function_calls.get(function_call_id)
# Necesitas saber el nombre de la función (puede venir en otro evento previo o en el output)
# Si tienes el nombre, ejecuta la función:
function_to_call = TOOL_FUNCTIONS.get(function_name)
if function_to_call:
if inspect.iscoroutinefunction(function_to_call):
result = await function_to_call(**function_args)
else:
result = function_to_call(**function_args)
else:
result = {"error": f"Función {function_name} no implementada"}
print(f"[FUNCALL] Output de la función {function_name}: {result}")
# Submit tool outputs
client.responses.submit_tool_outputs(
response_id=event.response_id,
tool_outputs=[{
"tool_call_id": function_call_id,
"output": json.dumps(result)
}]
)
yield f"event: {event_type}\ndata: {event.model_dump_json()}\n\n"
await asyncio.sleep(0)
Can anybody help me?