Skip to main content

Basic Execution

from gravixlayer import GravixLayer

client = GravixLayer()
runtime = client.runtime.create(template="python-3.12-base-small")

result = client.runtime.run_code(
    runtime.runtime_id,
    code="import math\nprint(math.sqrt(81))",
)
print(result.text)  # 9.0

client.runtime.kill(runtime.runtime_id)

Error Handling

from gravixlayer import GravixLayer

client = GravixLayer()
runtime = client.runtime.create(template="python-3.12-base-small")

result = client.runtime.run_code(runtime.runtime_id, code="raise ValueError('example')")

if not result.success and result.error:
    print(result.error.name)       # ValueError
    print(result.error.value)      # example
    print(result.error.traceback)  # Full traceback

client.runtime.kill(runtime.runtime_id)

With Code Context

Preserve variables across multiple run_code calls:
from gravixlayer import GravixLayer

client = GravixLayer()
runtime = client.runtime.create(template="python-3.12-base-small")

# Create a persistent context
ctx = client.runtime.create_code_context(runtime.runtime_id)

# First call sets a variable
client.runtime.run_code(runtime.runtime_id, code="x = 42", context_id=ctx.context_id)

# Second call reads it
result = client.runtime.run_code(runtime.runtime_id, code="print(x + 1)", context_id=ctx.context_id)
print(result.text)  # 43

client.runtime.kill(runtime.runtime_id)

Parameters

ParameterTypeRequiredDescription
runtime_idstringYesRuntime identifier
codestringYesPython source code to execute
context_idstringNoReuse a persistent code context
environmentobjectNoPer-execution environment variables
timeoutintegerNoExecution timeout in seconds

Response

FieldTypeDescription
textstringCombined stdout output
successbooleanTrue if no execution error
errorobjectError details (name, value, traceback)
resultslistStructured execution results
logsobjectstdout and stderr as lists of strings