Prompt template · Code
Prompt to convert code to another language
Translate code idiomatically into another language, map libraries, and list every behaviour difference. Free to copy, or open it in the prompt builder to fill the variables and get the exact API request.
233 tokens unfilled · 278 with the example below · o200k_base (GPT-4o and later)
Template
The prompt
Paste this into a chat window and replace each {{variable}}, or open it in the prompt builder to fill them in a form. Sections are marked with XML tags, which every major model reads well.
You are fluent in both {{from_language}} and {{to_language}}, and you write idiomatic {{to_language}} rather than transliterated {{from_language}}.
<context>
<source_code language="{{from_language}}">
{{code}}
</source_code>
</context>
<constraints>
- Use {{to_language}} idioms, standard library and error handling conventions, not a line-by-line copy.
- Target environment: {{target}}. Use only libraries available there; name any dependency you add.
- Keep behaviour the same, including edge cases: integer division, string encoding, null handling, sorting stability, time zones.
- Where the languages genuinely differ and identical behaviour isn’t possible or sensible, choose the idiomatic option and list the difference.
</constraints>
<output_format>
1. The converted code in one block.
2. A table of library mappings (source → target).
3. A list headed “Behaviour differences”, one line each, or “None found”.
</output_format>
<task>
Convert this code to {{to_language}}. The result should behave the same and read as if a {{to_language}} developer wrote it.
</task>For an API call: system prompt and user message
With a system prompt, the role, constraints and output format stay fixed across calls (and can be cached), while the context and task go in the user message.
You are fluent in both {{from_language}} and {{to_language}}, and you write idiomatic {{to_language}} rather than transliterated {{from_language}}.
<constraints>
- Use {{to_language}} idioms, standard library and error handling conventions, not a line-by-line copy.
- Target environment: {{target}}. Use only libraries available there; name any dependency you add.
- Keep behaviour the same, including edge cases: integer division, string encoding, null handling, sorting stability, time zones.
- Where the languages genuinely differ and identical behaviour isn’t possible or sensible, choose the idiomatic option and list the difference.
</constraints>
<output_format>
1. The converted code in one block.
2. A table of library mappings (source → target).
3. A list headed “Behaviour differences”, one line each, or “None found”.
</output_format><context>
<source_code language="{{from_language}}">
{{code}}
</source_code>
</context>
<task>
Convert this code to {{to_language}}. The result should behave the same and read as if a {{to_language}} developer wrote it.
</task>Variables
What to put in each variable
- {{from_language}}
- The source language, such as Python.
- {{to_language}}
- The target language, such as TypeScript.
- {{code}}
- The code to convert. Smaller units convert better than whole programs.
- {{target}}
- Where the result will run, such as “Node.js 22, ESM, no new npm packages” or “Go 1.24 standard library only”.
Use
When to use it
Use it for porting a utility module, moving a script into your main codebase’s language, or translating a reference implementation from documentation. It handles self-contained functions and modules well; for whole applications, convert one module at a time and keep the interfaces between them stable.
Conversions fail quietly on semantics, not syntax. Python’s // rounds toward negative infinity while integer division in many languages truncates; JavaScript sorts numbers as strings by default. Run the original’s tests against the converted code before trusting it.
Structure
Why it’s structured this way
Naming both languages in the role, with “idiomatic rather than transliterated”, is the core instruction. Without it, models tend to keep the source language’s shape: Python-style loops in Go, Java-style classes in TypeScript. The source code is tagged with its language so the model never confuses which side is which.
The target environment matters as much as the language. A conversion for Node.js may use built-ins a browser lacks, and “no new dependencies” is a real constraint in many codebases. The list of edge cases names the categories where languages most often differ, which prompts the model to check each one.
The behaviour-differences list makes the unavoidable trade-offs visible. Sometimes the idiomatic version should differ (raising an exception versus returning an error value), and you want that decision written down, not buried in the code.
Example
A filled-in example
- {{from_language}}
Python
- {{to_language}}
TypeScript
- {{code}}
def bucket(values: list[int], size: int) -> dict[int, list[int]]: out: dict[int, list[int]] = {} for v in sorted(values): out.setdefault(v // size, []).append(v) return out- {{target}}
Node.js 22, ESM, strict TypeScript, no new npm packages
What to look for. Two traps here: sorted must become a numeric sort ((a, b) => a - b), and Python’s floor division needs Math.floor(v / size), which differs from truncation for negative numbers. A good answer handles both and mentions them under behaviour differences.
The complete filled-in prompt (278 tokens)
You are fluent in both Python and TypeScript, and you write idiomatic TypeScript rather than transliterated Python.
<context>
<source_code language="Python">
def bucket(values: list[int], size: int) -> dict[int, list[int]]:
out: dict[int, list[int]] = {}
for v in sorted(values):
out.setdefault(v // size, []).append(v)
return out
</source_code>
</context>
<constraints>
- Use TypeScript idioms, standard library and error handling conventions, not a line-by-line copy.
- Target environment: Node.js 22, ESM, strict TypeScript, no new npm packages. Use only libraries available there; name any dependency you add.
- Keep behaviour the same, including edge cases: integer division, string encoding, null handling, sorting stability, time zones.
- Where the languages genuinely differ and identical behaviour isn’t possible or sensible, choose the idiomatic option and list the difference.
</constraints>
<output_format>
1. The converted code in one block.
2. A table of library mappings (source → target).
3. A list headed “Behaviour differences”, one line each, or “None found”.
</output_format>
<task>
Convert this code to TypeScript. The result should behave the same and read as if a TypeScript developer wrote it.
</task>Models
Tips by model family
- All models
Convert the tests too, separately and first. Running the original tests against the new code is the fastest way to find semantic differences the model didn’t mention.
- Gemini
Google recommends few-shot examples for Gemini. If you convert many files in one codebase, include one already-converted pair as an example so naming and error-handling choices stay consistent.
- Claude
Anthropic suggests XML tags for mixed content. Keep the
languageattribute on the source tag when you paste several files, so each one is clearly labelled.
Related
Related templates and tools
- Refactor codeRestructure code toward one stated goal without changing behaviour, with risky steps flagged.
- Write unit testsTests for normal cases, edge cases and errors in your framework, focused on behaviour rather than implementation.
- Explain codeA plain-English walkthrough of unfamiliar code at the reader’s level: purpose, flow, side effects and traps.
Edited the template? Compare your version with this one in the prompt diff, and count it for your model with the token counter. Browse all 29 prompt templates.