How to Prompt AI to Port Code to Another Language
Line-by-line translation produces code that compiles and misbehaves. Prompt for behaviour and test cases instead, and check the traps that differ between languages.
To prompt AI to port code to another language, describe what the code must do and give it test cases that both versions must pass, rather than asking for a line-by-line translation. A literal translation often compiles and then behaves differently, because languages disagree about numbers, empty values, dates and sorting.
Why direct translation goes wrong
"Convert this Python to JavaScript" gets you syntax that looks right. The problem is meaning. The same expression can give a different answer in another language, and nothing will warn you. Treat a port as a rewrite that must match behaviour, and prove the match with tests.
Step 1: Have the AI write the behaviour down first
Before any translation, ask for a plain description of what the source code does. This also surfaces behaviour you did not know it had.
Read the code below. Do not translate it yet.
List: inputs and outputs with types, side effects (files, network, database),
error cases and how they are signalled, and any behaviour that depends on
language details (integer division, null handling, date handling, sorting).Check the list against what you know. Fix it before moving on. If the code is hard to follow, prompting AI to explain code line by line helps.
Step 2: Create shared test vectors
Pick 10 to 20 inputs with the expected outputs, including odd ones: empty input, zero, negative numbers, very large numbers, unusual characters and a date at a month boundary. Run the original code to produce the outputs, and store them as a simple data file. This file is the contract, and both versions must pass it.
Step 3: Port in small pieces
Port only the function parse_invoice from Python to TypeScript.
Match the behaviour list above. Do not use new libraries unless I approve.
Write a test file that loads test-vectors.json and checks every case.One function at a time keeps the diff reviewable. For bigger jobs, handing a code migration to an AI coding agent covers working in stages, and reviewing a multi-file refactor from AI covers checking the result.
Language traps to check by hand
Area | Example trap |
|---|---|
Integer division | Python's |
Big numbers | Python integers grow without limit, while JavaScript numbers lose integer exactness above Number.MAX_SAFE_INTEGER, which is 2^53 minus 1 |
Empty values | None, null, undefined and an empty string are different things, and each language treats them in truthiness checks differently |
Dates | Month numbering and time zone defaults differ between libraries, so a date near midnight can shift a day |
Sorting | Default sort order for mixed case or numbers as text can differ |
Text | Length of a string can count bytes, code units or characters, depending on the language |
Ask the AI directly: "Which of these traps appear in this code, and how did you handle each?" Then verify with a test, not with its explanation.
Step 4: Run both versions side by side
Feed the same inputs to the old and new code and compare outputs. Differences are your bug list. Resist the urge to edit the test vectors to make the port pass, unless the old behaviour was itself the bug, and then make that a deliberate decision.
Step 5: Keep it idiomatic
After behaviour matches, ask for a second pass to make the code natural in the target language, such as using its standard library instead of hand-rolled helpers. Run the same tests again. For a narrower, well-trodden case, see converting JavaScript to TypeScript with AI.
For the general habits behind prompts like these, see prompt engineering.
FAQ
Can AI translate code from one language to another?
Yes, and it often produces working code. The risk is subtle behaviour differences, so verify with tests that both versions pass.
What is the best prompt for converting code?
One that asks for a behaviour description first, then a port of one function at a time with shared test cases.
Should I port a whole project in one go?
No. Small pieces keep each change reviewable and let tests catch drift early.
How do I know the ported code behaves the same?
Run the same set of inputs through both versions and compare outputs, including edge cases.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


