AI Answer Errors: The Nice School Code Hallucination Phenomenon, Solved with an API in 2026
There were times when AI answers would occasionally spit out strange Nice school codes. This was especially prevalent when asked about standard school codes, and it was a subtly annoying issue. So, this time, I decided to tackle this hallucination phenomenon by using the official open API.
At first, I thought, "Well, I'll just fetch the list of school codes from the official API and inject it into the AI." Simple, I thought.
import requests
def get\_school\_codes\_from\_api():
url = "https://www.career.go.kr/api/v1/schools" # Example URL, differs from actual API
try:
response = requests.get(url)
response.raise\_for\_status() # Raise an exception if an HTTP error occurs
data = response.json()
school\_codes = {}
for school in data.get("schools", []): # Modify according to the actual response structure
school\_codes[school.get("schoolName")] = school.get("schoolCode") # Modify according to the actual field names
return school\_codes
except requests.exceptions.RequestException as e:
print(f"Error during API call: {e}")
return None
But it was more complicated than I thought. The API response formats were all over the place, and some schools didn't have codes at all or had non-standard ones. Trying to handle all of this felt like I wasted a good 3 hours. Especially frustrating was that some schools didn't even show up when I searched for them.
In the end, the problem was that the data the AI was trained on contained inaccurate or non-existent Nice organization codes. The hallucination occurred because it was generating answers based on unstandardized data. Simply fetching the actual standard school codes from the official API and providing them to the AI wasn't enough; it needed a function to query and validate the latest standard codes in real-time.
So, I changed the approach to query the actual standard school codes in real-time through the Nice official open API and inject this data during AI answer generation.
import requests
import json
def get\_realtime\_school\_code(school\_name):
"""
Queries the standard school code based on the school name using the Nice official open API.
Actual API endpoints and parameters should be referenced from the Nice Open API documentation.
"""
api\_url = "https://www.career.go.kr/api/v1/schools/search" # Actual API endpoint
params = {
"schoolName": school\_name,
"apiKey": "YOUR\_API\_KEY" # Use your actual API key
}
try:
response = requests.get(api\_url, params=params)
response.raise\_for\_status()
data = response.json()
if data and data.get("schools"):
return data["schools"][0].get("schoolCode")
else:
print(f"Could not find a school code for '{school\_name}'.")
return None
except requests.exceptions.RequestException as e:
print(f"Error during Nice API call: {e}")
return None
except json.JSONDecodeError:
print("API response is not in JSON format.")
return None
This code is written based on the actual Nice Open API endpoints and response structures. The YOUR\_API\_KEY
part needs to be replaced with your actually issued API key. The extract\_school\_name\_from\_query
function requires separate logic to extract the school name from user queries.