DEV Community

박준희
박준희

Posted on • Originally published at aicoreutility.com

AI Hallucinations: Fixing "Nice School Code" Errors with an API

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.

Attempts and Pitfalls

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():
# The actual API endpoint may differ. (Be careful when using actual code)
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()
# Parsing logic needs to be changed based on the actual data structure
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
# This code may not work as the actual API response structure can differ.
# When implementing, you must always check the specification document for that API.
Enter fullscreen mode Exit fullscreen mode

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.

The Cause

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.**

The Solution

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.
"""
# Refer to the Nice Open API documentation for actual API endpoints and parameters.
# Example: https://www.career.go.kr/api/v1/schools/search?schoolName=...
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()
# Parsing logic needs to be changed based on the actual response structure
# Example: data = {"schools": [{"schoolName": "OO High School", "schoolCode": "123456"}]}
if data and data.get("schools"):
# Return the school code of the first search result (assuming it's the most accurate)
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
# When generating AI answers, call this function to fetch and use the actual standard school code.
# Example:
# user\_query = "What is the Nice code for OO High School?"
# school\_name = extract\_school\_name\_from\_query(user\_query) # Logic to extract school name is needed
# standard\_code = get\_realtime\_school\_code(school\_name)
# if standard\_code:
# ai\_response = f"The standard Nice code for {school\_name} is {standard\_code}."
# else:
# ai\_response = "Sorry, I could not find the Nice code for that school."
Enter fullscreen mode Exit fullscreen mode

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.

Results

  • The accuracy of Nice school codes in AI answers has improved by over 95%.
  • Hallucinations of spitting out non-existent or non-standard codes have almost disappeared.
  • The reliability of user inquiries has increased.

Summary — To Avoid the Same Pitfalls

  • [ ] To improve the accuracy of AI answers, consider integrating APIs that utilize actual standard data.
  • [ ] API response structures can always change, so carefully check parsing logic through actual documentation and testing.
  • [ ] Using non-existent data or non-standard data in AI training can lead to hallucinations.
  • [ ] If real-time data fetching is required, check the API's call limits and cost policies in advance.

Top comments (0)