Working with the OpenAI Responses API
James Chapman
AI Curriculum Manager, DataCamp
convert_currency()amount: amount of moneyfrom_currency: original currencyto_currency: target currency






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)}"
print(convert_currency(100, "USD", "EUR"))
100 USD = 86.62 EUR (Rate: 0.86625)
Working with the OpenAI Responses API