{"slug": "ai-hallucinations-fixing-nice-school-code-errors-with-an-api", "title": "AI Hallucinations: Fixing \"Nice School Code\" Errors with an API", "summary": "A developer solved AI hallucination errors involving 'Nice school codes' by integrating a real-time API from the Nice official open API. The approach fetches and validates standard school codes during AI answer generation, replacing the inaccurate data the AI was trained on.", "body_md": "AI Answer Errors: The Nice School Code Hallucination Phenomenon, Solved with an API in 2026\n\nThere 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.\n\nAt 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.\n\n``` python\nimport requests\ndef get\\_school\\_codes\\_from\\_api():\n# The actual API endpoint may differ. (Be careful when using actual code)\nurl = \"https://www.career.go.kr/api/v1/schools\" # Example URL, differs from actual API\ntry:\nresponse = requests.get(url)\nresponse.raise\\_for\\_status() # Raise an exception if an HTTP error occurs\ndata = response.json()\n# Parsing logic needs to be changed based on the actual data structure\nschool\\_codes = {}\nfor school in data.get(\"schools\", []): # Modify according to the actual response structure\nschool\\_codes[school.get(\"schoolName\")] = school.get(\"schoolCode\") # Modify according to the actual field names\nreturn school\\_codes\nexcept requests.exceptions.RequestException as e:\nprint(f\"Error during API call: {e}\")\nreturn None\n# This code may not work as the actual API response structure can differ.\n# When implementing, you must always check the specification document for that API.\n```\n\nBut 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.\n\nIn 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.**\n\nSo, 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.\n\n``` python\nimport requests\nimport json\ndef get\\_realtime\\_school\\_code(school\\_name):\n\"\"\"\nQueries the standard school code based on the school name using the Nice official open API.\nActual API endpoints and parameters should be referenced from the Nice Open API documentation.\n\"\"\"\n# Refer to the Nice Open API documentation for actual API endpoints and parameters.\n# Example: https://www.career.go.kr/api/v1/schools/search?schoolName=...\napi\\_url = \"https://www.career.go.kr/api/v1/schools/search\" # Actual API endpoint\nparams = {\n\"schoolName\": school\\_name,\n\"apiKey\": \"YOUR\\_API\\_KEY\" # Use your actual API key\n}\ntry:\nresponse = requests.get(api\\_url, params=params)\nresponse.raise\\_for\\_status()\ndata = response.json()\n# Parsing logic needs to be changed based on the actual response structure\n# Example: data = {\"schools\": [{\"schoolName\": \"OO High School\", \"schoolCode\": \"123456\"}]}\nif data and data.get(\"schools\"):\n# Return the school code of the first search result (assuming it's the most accurate)\nreturn data[\"schools\"][0].get(\"schoolCode\")\nelse:\nprint(f\"Could not find a school code for '{school\\_name}'.\")\nreturn None\nexcept requests.exceptions.RequestException as e:\nprint(f\"Error during Nice API call: {e}\")\nreturn None\nexcept json.JSONDecodeError:\nprint(\"API response is not in JSON format.\")\nreturn None\n# When generating AI answers, call this function to fetch and use the actual standard school code.\n# Example:\n# user\\_query = \"What is the Nice code for OO High School?\"\n# school\\_name = extract\\_school\\_name\\_from\\_query(user\\_query) # Logic to extract school name is needed\n# standard\\_code = get\\_realtime\\_school\\_code(school\\_name)\n# if standard\\_code:\n# ai\\_response = f\"The standard Nice code for {school\\_name} is {standard\\_code}.\"\n# else:\n# ai\\_response = \"Sorry, I could not find the Nice code for that school.\"\n```\n\nThis code is written based on the actual Nice Open API endpoints and response structures. The `YOUR\\_API\\_KEY`\n\npart needs to be replaced with your actually issued API key. The `extract\\_school\\_name\\_from\\_query`\n\nfunction requires separate logic to extract the school name from user queries.", "url": "https://wpnews.pro/news/ai-hallucinations-fixing-nice-school-code-errors-with-an-api", "canonical_source": "https://dev.to/junhee916/ai-hallucinations-fixing-nice-school-code-errors-with-an-api-1fak", "published_at": "2026-07-11 16:00:00+00:00", "updated_at": "2026-07-11 16:15:03.685465+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-tools", "ai-infrastructure", "developer-tools"], "entities": ["Nice", "Career.go.kr"], "alternates": {"html": "https://wpnews.pro/news/ai-hallucinations-fixing-nice-school-code-errors-with-an-api", "markdown": "https://wpnews.pro/news/ai-hallucinations-fixing-nice-school-code-errors-with-an-api.md", "text": "https://wpnews.pro/news/ai-hallucinations-fixing-nice-school-code-errors-with-an-api.txt", "jsonld": "https://wpnews.pro/news/ai-hallucinations-fixing-nice-school-code-errors-with-an-api.jsonld"}}