Skip to content

Translate a Python source file

POST
/v1/translate
curl --request POST \
--url https://api.legesher.io/v1/translate \
--header 'Content-Type: application/json' \
--data '{ "enhanced": false, "python_version": "3.10", "source": "example", "source_lang": "example", "target_lang": "example" }'

Translate a complete Python source file between natural languages.

Supports en→X, X→en, and X→Y (pivoted through English). With enhanced: true (any paid plan), a model pass additionally translates the extracted comments, docstrings, string literals, and identifier proposals, once, after the pivot.

Media type application/json
TranslateFileRequest

A complete Python source file to translate.

object
enhanced
Enhanced

Request AI-enhanced translation: after the deterministic pack pass, a model pass translates the extracted comments, docstrings, string literals, and identifier proposals (collision-validated against the pack). Gated to paid plans, Individual and above (402 tier_required on Free); degrades gracefully to standard with mode_fallback=true when the model layer is unavailable.

boolean
python_version
Python Version

Python grammar version used to select the language pack.

string
default: 3.13
Allowed values: 3.10 3.11 3.12 3.13 3.14
source
required
Source

Python source text to translate.

string
>= 1 characters <= 2000000 characters
source_lang
required
Source Lang

Language the source is written in (ISO 639 code).

string
>= 2 characters <= 16 characters /^[a-z]{2,3}(-[A-Za-z0-9]+)?$/
target_lang
required
Target Lang

Language to translate into (ISO 639 code).

string
>= 2 characters <= 16 characters /^[a-z]{2,3}(-[A-Za-z0-9]+)?$/

Successful Response

Media type application/json
TranslateResponse

Translated source plus the parameters that produced it.

object
fallback_reason
Fallback Reason

Machine-readable reason when mode_fallback is true: ‘not_configured’, ‘enhanced_runtime_unavailable’, ‘model_budget_exhausted’, or ‘model_provider_unavailable’ (the provider-failure code).

Any of:
string
metadata

Element-level metadata (cultural comments; enhanced element counts and identifier glossary when the model pass ran); null when there is none to report.

Any of:
TranslateMetadata

Element-level metadata for one translation.

Everything here is about the translation the caller just received, derived from their own submission, never from any other caller’s data.

object
cultural_comments
Cultural Comments

Engine cultural-comment metadata (#@culture[lang]: …) found in the source, keyed by line; null when the source carries none.

Any of:
object
key
additional properties
any
enhanced_elements

How many extracted elements the model pass covered, by kind; null unless the enhanced pass ran.

Any of:
EnhancedElementCounts

Elements the enhanced model pass submitted, by kind.

object
comments
Comments
integer
0
docstrings
Docstrings
integer
0
identifiers
Identifiers
integer
0
strings
Strings
integer
0
identifier_glossary
Identifier Glossary

Identifier translations applied by the enhanced pass (source identifier → translated identifier), collision-validated against the language pack; null unless the enhanced pass ran. With the opt-in X-Legesher-Repo header, later requests from the same caller for the same repository reuse this glossary for consistent naming; it is kept per caller and never shared with another.

Any of:
object
key
additional properties
string
mode_fallback
Mode Fallback

True when enhanced was requested but the model pass could not run (or failed) and the deterministic result is served instead.

boolean
mode_used
Mode Used

Which pipeline produced translated_source: ‘standard’ (deterministic pack pass) or ‘enhanced’ (pack pass + model pass over extracted elements).

string
default: standard
Allowed values: standard enhanced
pivoted
required
Pivoted

True when translated via the English pivot (X→en→Y).

boolean
python_version
required
Python Version
string
Allowed values: 3.10 3.11 3.12 3.13 3.14
source_lang
required
Source Lang
string
target_lang
required
Target Lang
string
translated_source
required
Translated Source
string

Example

{
"metadata": {
"enhanced_elements": {
"comments": 0,
"docstrings": 0,
"identifiers": 0,
"strings": 0
}
},
"mode_fallback": false,
"mode_used": "standard",
"python_version": "3.10"
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

An API key was sent and is not valid. A request with no key runs on the anonymous trial instead.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

Enhanced translation needs a paid plan (tier_required).

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

The language has no pack for the requested Python version.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

The request body or the source is over the size limit.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

A field is missing or malformed, or a language code is not supported.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

Too many requests, or this month’s character allowance is used up.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
Retry-After
integer

Seconds to wait before retrying. Usually sent with rate_limited; absent on quota_exceeded, which lasts until the UTC month resets.

X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.

The translation engine is unavailable.

Media type application/json
ErrorResponse

Envelope for every error response.

object
error
required
ErrorDetail

Machine-readable code + human message for one error.

object
code
required
Code

Machine-readable error code, e.g. ‘validation_error’, ‘unsupported_language’, ‘language_pack_not_found’, ‘payload_too_large’, ‘foundation_unavailable’, ‘invalid_source_code’, ‘internal_error’.

string
details
Details

Per-field breakdown (validation errors only).

Any of:
Array<object>
FieldIssue

One request-validation problem, stripped of the submitted input.

object
field
required
Field

Dotted location of the offending field.

string
message
required
Message

Human-readable description.

string
type
required
Type

Pydantic error type identifier.

string
message
required
Message

Human-readable explanation.

string

Example generated

{
"error": {
"code": "example",
"details": [
{
"field": "example",
"message": "example",
"type": "example"
}
],
"message": "example"
}
}
X-RateLimit-Limit
integer

Requests permitted per minute for the caller’s tier. The X-RateLimit-* set is omitted when the tier’s rate is unlimited, and on a request refused before the caller is identified.

X-RateLimit-Remaining
integer

Requests remaining in the current rate window.

X-RateLimit-Reset
integer

Seconds until the rate limit fully resets.

X-Request-ID
string

Correlation id for this request: the validated inbound X-Request-ID when one was sent, else a server-minted UUID.

X-Usage-Characters-Remaining
integer

Characters left in the caller’s monthly budget.

X-Usage-Characters-Used
integer

Characters billed against the caller’s monthly budget, UTC month to date. The X-Usage-* pair is omitted when the budget is unlimited, and on a request refused before the caller is identified.