Skip to content
AI Dev Toolkit.
Esc
  • Loading…

Prompt template · Code

Prompt to convert code to another language

A good prompt to convert code to another language names both languages and the target environment, then asks for idiomatic code rather than a line-by-line copy. This template keeps behaviour the same through edge cases such as integer division and sorting, returns a table of library mappings, and lists every behaviour difference it couldn’t avoid.

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 code conversion prompt template (copy it)

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.

Prompt (one message, plain text)
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.

System prompt
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>
User message
<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 do the variables mean?

{{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 should I use this code conversion prompt?

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 is the prompt 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

Example: a filled-in code conversion prompt

{{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)
Filled-in prompt
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

Does it work the same in ChatGPT, Claude and Gemini?

Yes, the structure works in all three: tagged sections that keep your material apart from the instructions, and an explicit output format. The tips below cover where the providers’ own guides differ for this code conversion prompt.

  • 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 language attribute on the source tag when you paste several files, so each one is clearly labelled.

FAQ

Questions people ask

Can ChatGPT convert Python to JavaScript?

Yes, for self-contained functions and modules. Conversions fail on meaning more than syntax: Python’s // rounds toward negative infinity while integer division in many languages truncates, and JavaScript sorts numbers as strings by default. Convert the tests too, and run the original tests against the new code before you trust it.

How do I get idiomatic code, not a literal translation?

Say so in the role, and name the target environment. This template asks for code that uses the target language’s idioms, standard library and error handling conventions. Naming the runtime, such as Node.js 22 with no new npm packages, also stops the model reaching for libraries you can’t use.

What usually breaks when converting code?

Semantics the syntax hides: integer division and rounding, string encoding, null handling, sort stability, time zones, and exceptions versus returned errors. The template names these so the model checks each one, and asks for a “Behaviour differences” list so any deliberate change, such as raising instead of returning an error, is written down.

Related

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.

Keep exploring