Evaluating LLMs on Polyglot Code: A Reproducible Testing Methodology
Learn how to evaluate whether large language models can correctly analyze polyglot files—code that runs under both Python and Node.js. This article provides a working sample, a step-by-step evaluation prompt, a 0–2 scoring rubric, and a recording template so you can reproduce the tests with your own models and environment, without relying on published rankings.
Evaluating LLMs on Polyglot Code: A Reproducible Testing Methodology
In real-world projects, it is common for a single file to mix multiple languages—build scripts, template files, or configuration files with embedded code are typical examples. Evaluating whether a large language model can handle such files comes down to two questions: can it determine which language a given code segment belongs to, and can it explain how the two languages’ execution paths diverge? Polyglot samples are designed to test exactly this. Rather than publishing model rankings, this article provides the sample, the scoring method, and a recording template, so you can repeat the tests with your own models and environment.
1. Test Sample
The following code can be executed by both the Python interpreter and Node.js, with each runtime printing the same single line of output.
1//1;"""console.log("Hello, polyglot");/*"""print("Hello, polyglot")#*/
Run it as follows. Use Python 3.8 or later and Node.js 18 or later. Run it in a directory that does not contain a "type": "module" setting, so Node.js does not treat the file as an ES module.
python3 polyglot.py # Output: Hello, polyglotnode polyglot.py # Output: Hello, polyglot
Here is how each runtime executes the file:
- Line 1:
1//1;""". In Python,1//1is integer division with a result of 1, which is discarded; the following"""then starts a multi-line string. In JavaScript, everything after//on this line is a comment, so the line has no effect on JavaScript. - Line 2. Python treats it as string content and ignores it. JavaScript executes
console.logand prints the text. - Line 3:
/*""". In Python, the"""here closes the string opened on line 1, and/*is merely string content. In JavaScript,/*opens a block comment, and the"""is commented out. - Line 4. Python executes
printand prints the text. JavaScript is still inside the block comment and ignores this line. - Line 5:
#*/. Python treats everything after#as a comment. In JavaScript,*/closes the block comment; the preceding#is also inside a comment and causes no syntax error.
The key point is that each language executes only its own segment. 1//1 is a valid Python expression and simultaneously the start of a JavaScript comment—this line is the boundary between the two languages.
2. Evaluation Design
For each sample, the model should answer three types of questions. First, can the file be executed by both languages? Second, which syntax differences does it exploit? Third, in what order does each engine execute the code, and what content is skipped?
We recommend the following prompt, which requires the model to analyze step by step rather than jumping to a conclusion:
Here is a file. Please answer in the following steps:1. Identify which languages' syntax it contains;2. Explain line by line what the Python interpreter and the JavaScript engine each do;3. Give the output of each runtime.File content:[paste the sample code here]
Scoring uses a 0–2 scale, with each dimension scored independently.
| Dimension | 2 points | 1 point | 0 points |
|---|---|---|---|
| Language identification | Correctly identifies both languages | Identifies only one language | Incorrect judgment |
| Mechanism explanation | Explains the roles of // and /* |
Explains only part of the mechanism | Incorrect mechanism explanation |
| Execution path | Describes both engines’ behavior line by line | Gives only the overall conclusion | Incorrect execution order |
| Consistency under follow-up | Conclusions remain stable across follow-up questions | Minor inconsistencies in details | Conclusions overturned or rewritten under follow-up |
| Hallucination | No fabricated content | Fabricated details that do not affect the conclusion | Fabricated execution logic that does not exist |
Run each model at least three times and record the mean and minimum scores. For every test run, record the following configuration: model name and version, quantization method, hardware model, context length, temperature, random seed, and test date. Results lacking this information should not be used directly as a basis for model selection.
3. Observations
In a manual test, we made qualitative observations of several models. First, some models accurately identified the roles of 1//1 and the block comment, and correctly explained which parts each engine ignores. Second, one model answered correctly in the first round but rewrote its explanation under later follow-up questions, showing that consistency across long conversations needs to be checked separately. Third, one larger-parameter local model fabricated a global execution order for the entire file—an order that looked plausible but did not match the actual behavior of either engine.
These are observations from single sessions with a very small sample size; they say nothing about the models’ overall capability, so this article provides no percentages or rankings. The cause of the second phenomenon can currently only be inferred from the output—for example, interference from comment text in the context. Confirming the cause would require inspecting inference logs or running ablation experiments with reduced context.
Inference speed for local deployment was not measured in this article. If you need this data, record the hardware, quantization method, input and output lengths, and the timing method at the same time; otherwise the numbers are not comparable.
The term “multi-source information” used here refers to the joint processing of three kinds of information—code text, comments, and syntax structure—and has nothing to do with multimodal inputs such as images or audio.
4. Selection and Usage Recommendations
Before selecting a model, define the weights for each criterion based on your task. Below is a reference framework.
- Code review: accuracy carries the highest weight, and conclusions require human review.
- Real-time completion: latency carries the highest weight, and partially imperfect suggestions are acceptable.
- Documentation generation: accuracy and cost carry similar weight, making local or private environments a good fit.
Keep the following points in mind during use.
- Even if a model’s explanation is well organized, verify it line by line against the sample rather than accepting it directly.
- If the code contains keys, credentials, or private repository content, sanitize it before sending it to a cloud-hosted model.
- Treat a model’s high confidence in its analysis with skepticism and arrange for human review.
5. Limitations
This article uses only one Python–JavaScript sample. It cannot represent other language pairs or other types of mixed-language code. To obtain a reliable basis for model selection, teams should build samples from their own codebases and test repeatedly using the method described in Section 2.
