chatGPT code hallucinations

By this point, many people understand the concept of an AI “hallucination”. This is the term that has come to describe incorrect information stated as facts in the output of a chatGPT prompt.

For example, if you ask “Who is Brian Toone?” to chatGPT, you get the response below which has some correct information, but several “hallucinations” as well.

Brian Toone is an American mathematician, cyclist, and computer scientist. He is a professor of computer science at the University of South Alabama [Samford University] and has published research on topics such as computational geometry, robotics, and algorithmic game theory [web engineering, data analysis, security, data trustworthiness]. Toone is also an accomplished cyclist and has completed a number of ultra-endurance cycling events, including the Race Across America and the Tour Divide. He is known for his use of technology to enhance his cycling performance, including building his own [software for] bike computers and GPS tracking devices.

chatGPT response to the prompt “who is brian toone?”

I’ve modified the output to use strikethrough font to point out the hallucinations with the correction (if applicable) in [brackets].

As it turns out, chatGPT can generate equivalent code hallucinations as well. I am currently working on connecting the Strava API to my bicycle radar project (mybiketraffic.com). I’ve explored and used the API many times before on other projects, but never integrated it into mybiketraffic because I knew that it doesn’t allow you to download the original .FIT file, which is the only place where my radar data is stored.

I was looking through the latest documentation for the API just to make sure that the API hadn’t been updated to include that capability. I wasn’t finding anything, so I decided to ask chatGPT. It gives me perfectly reasonable code for an API endpoint that does indeed exist. But that endpoint doesn’t take in a parameter called “original”. It is a perfectly reasonable way they could have implemented the API for downloading the original FIT file, but that parameter doesn’t exist in the Strava API. This is my first encounter with a code hallucination.

import requests

access_token = ‘your_access_token_here’
activity_id = ‘your_activity_id_here’

url = f’https://www.strava.com/api/v3/activities/{activity_id}/streams/original?access_token={access_token}’

response = requests.get(url)

with open(‘activity.fit’, ‘wb’) as f:
f.write(response.content)

chatGPT response to the prompt “how can i download the original fit file using the strava api?”. The hallucination is the parameter “original”, which doesn’t exist in the API. Also, I know from experience working with the API that you have to do a bit more work to get the correct token, and it’s not passed as part of the query string.

In conclusion, chatGPT produces very nice, clean, well-documented code, but you cannot trust it. You still need to know how to code and potentially do a lot of debugging and research to “tweak” the code to make it work. Most of the time, it would be faster to write the code from scratch yourself! The danger with a code hallucination such as the one documented above is that there is NO AMOUNT OF TWEAKING that will make the code work. You simply cannot do what I asked it to do.

Leave a comment

Your email address will not be published. Required fields are marked *