Coding
AI Prompt for Language Conversion
Convert code between languages with this AI prompt: idiomatic translation, semantic differences flagged, and equivalence tests to prove it works.
The prompt
Role: You are a polyglot engineer who translates code between languages faithfully AND idiomatically. Context: From: [FROM_LANGUAGE] ([FROM_VERSION]). To: [TO_LANGUAGE] ([TO_VERSION]). Target runtime: [RUNTIME]. Code: [CODE] Task: 1) Translate to idiomatic [TO_LANGUAGE] — not transliteration: use its standard library, idioms, and conventions (a Pythonista should not recognize the Python underneath). 2) List EVERY semantic difference: integer division, string encoding, truthiness, error handling, concurrency model, number precision. 3) Map library dependencies: [FROM_LANGUAGE] lib → [TO_LANGUAGE] equivalent (or "no equivalent — here's the approach"). 4) Provide test cases that verify behavioral equivalence, runnable in [TO_LANGUAGE]. Format: Translated code first, then the semantic-differences table (difference | risk | how verified), then the dependency map, then tests. Ask the AI to flag the 'uncanny valley' constructs: code that runs in the target language but betrays its origin through unidiomatic patterns. Idiomatic output is the difference between translated and native. Constraints: Idiomatic output — no "Python written in JavaScript." Flag untranslatable constructs loudly instead of approximating silently. The tests must actually run in [TO_LANGUAGE].
Variables to fill in
Replace each [variable] in the prompt with your own details:
[FROM_LANGUAGE] [FROM_VERSION] [TO_LANGUAGE] [TO_VERSION] [RUNTIME] [CODE]
Example input
FROM=Python, TO=JavaScript (Node.js), CODE=Python data processing script using pandas, IDIOMS=use native array methods, no lodash, TARGET_RUNTIME=Node 20
How to use this prompt
Name exact versions — standard libraries differ by version. Demand the semantic-differences list; direct translation hides behavior changes.
Tips for the best results
- Review the semantic-differences table first — that is where conversion bugs live.
- Test number-heavy code extra carefully; precision semantics differ most there.
- Ask for the dependency map before the code when libraries are involved.
Frequently asked questions
Will the result look like native code?
It translates logic faithfully AND re-expresses it idiomatically — then lists every semantic difference for you to verify.
How do I handle libraries with no equivalent?
The prompt's dependency map offers the closest approach per library — sometimes a different architecture, not a direct port. Budget time for those.