Working with the OpenAI Responses API
James Chapman
AI Curriculum Manager, DataCamp
def convert_currency(amount, from_currency, to_currency):
url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
rate = data['rates'].get(to_currency)
if rate is None:
return f"Could not find exchange rate for {from_currency} to {to_currency}"
converted_amount = amount * rate
return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"
except requests.exceptions.RequestException as e:
return f"Error converting currency: {str(e)}"

tools = [{ "type": "function", "name": "convert_currency", "description": "Convert an amount from one currency to another using real-time exchange rates.","parameters": { "type": "object","properties": {"amount": {"type": "number", "description": "The amount of money to convert"}, "from_currency": {"type": "string", "description": "The source currency code (e.g., 'USD', 'EUR')"}, "to_currency": {"type": "string", "description": "The target currency code (e.g., 'USD', 'EUR')"}},"required": ["amount", "from_currency", "to_currency"], "additionalProperties": False}}]
response = client.responses.create( model="gpt-5-mini", input=[{"role": "user", "content": "How much is $100 in euros?"}], tools=tools )for item in response.output: print(item)
ResponseReasoningItem(id='rs_0a46aaf8050644c4006924978291a481909d5f50b8b9f7e959',
summary=[], type='reasoning', content=None, encrypted_content=None, status=None)
ResponseFunctionToolCall(arguments='{"amount":100,"from_currency":"USD","to_currency":"EUR"}',
call_id='call_xTbWJuO6TiuFw84TK0hU9hRT', name='convert_currency',
type='function_call',
id='fc_0a46aaf8050644c40069249784fa248190874283efa2fc2c46', status='completed')
messages = [{"role": "user", "content": "How much is $100 in euros?"}]response = client.responses.create( model="gpt-5", input=messages, tools=tools )messages += response.output
for item in response.output: if item.type == "function_call": if item.name == "convert_currency":currency_result = convert_currency(**json.loads(item.arguments))messages.append({ "type": "function_call_output", "call_id": item.call_id, "output": json.dumps({"convert_currency": currency_result}) })response = client.responses.create( model="gpt-5-mini", instructions="Respond to the user input using the tool output.", tools=tools, input=messages, )
print(response.output_text)
100 USD = 86.62 EUR (rate used: 1 USD = 0.86625 EUR).
Note: exchange rates fluctuate and actual amounts may differ due to fees or the
provider you use. Want to convert a different amount or use a different currency?
Working with the OpenAI Responses API