跳到正文
DEV Community· Carlos Luque Zayas Bazan·· 3 小時前AI 評分39

為何 API 幻覺逃過 Linter,以及我如何建立解析器捕捉它們

Why API Hallucinations Are Invisible to Your Linter (And How I Built a Parser to Catch Them.

AI 導讀

作者以 Go 建立 CLI,解析 Python 程式碼並檢查呼叫的函式和方法是否存在,捕捉靜態分析工具容易漏掉的 API 幻覺。解析器採用純 Go 實作的 gotreesitter,避免 CGO 綁定在 Windows 上需要 MinGW;雖然速度較慢,但分析單一檔案只相差數毫秒。首個版本為三個程式庫硬編碼有效方法,程式碼目前尚未公開。

正文

Carlos Luque Zayas Bazan

When an AI coding assistant writes requests.get_json() instead of response.json(), the code compiles. Tests pass. Type checkers don't complain. SAST tools don't flag it. And yet the method doesn't exist.
This is an API hallucination. It's a specific failure mode of AI-generated code that most static analysis tools miss because they're looking for known vulnerabilities or suspicious patterns, not for calls to functions that were never real to begin with.
I've been building a CLI in Go that parses Python code and checks whether the functions and methods it calls actually exist in the library. The core idea is simple: if you know the valid API surface of requests, json, and os, you can flag requests.get_json() because get_json isn't in that surface.
The parsing is done with gotreesitter, a pure-Go implementation of tree-sitter. I chose it over the CGO binding because I needed to compile on Windows without MinGW. The tradeoff is performance (the pure-Go version is slower), but for a CLI that analyzes individual files, the difference is milliseconds.
The hard part isn't the parser. It's the registry of valid APIs. For the first version, I hardcoded the valid methods for three libraries. That's enough to demonstrate the concept, but it doesn't scale. Expanding it to pandas, numpy, or django requires either manual curation or extracting method names from type stubs.
What I've learned so far: API hallucinations are predictable. They follow patterns — plausible-sounding method names, slight variations on real APIs, invented functions that "should" exist. A deterministic checker can catch them if it knows what's real. The question is how to build that "what's real" database without drowning in maintenance.
The code isn't public yet, but I'm sharing the approach because I think the problem is worth solving. If you've seen API hallucinations in your own AI-generated code, I'd like to hear how you catch them.

來源:DEV Community · dev.to