InvokeCodeInterpreter - Amazon Bedrock AgentCore Data Plane

InvokeCodeInterpreter

Executes code within an active code interpreter session in Amazon Bedrock. This operation processes the provided code, runs it in a secure environment, and returns the execution results including output, errors, and generated visualizations.

To execute code, you must specify the code interpreter identifier, session ID, and the code to run in the arguments parameter. The operation returns a stream containing the execution results, which can include text output, error messages, and data visualizations.

This operation is subject to request rate limiting based on your account's service quotas.

The following operations are related to InvokeCodeInterpreter:

Request Syntax

POST /code-interpreters/codeInterpreterIdentifier/tools/invoke HTTP/1.1 x-amzn-code-interpreter-session-id: sessionId Content-type: application/json { "arguments": { "clearContext": boolean, "code": "string", "command": "string", "content": [ { "blob": blob, "path": "string", "text": "string" } ], "directoryPath": "string", "language": "string", "path": "string", "paths": [ "string" ], "taskId": "string" }, "name": "string" }

URI Request Parameters

The request uses the following URI parameters.

codeInterpreterIdentifier

The unique identifier of the code interpreter associated with the session. This must match the identifier used when creating the session with StartCodeInterpreterSession.

Required: Yes

sessionId

The unique identifier of the code interpreter session to use. This must be an active session created with StartCodeInterpreterSession. If the session has expired or been stopped, the request will fail.

Pattern: [0-9a-zA-Z]{1,40}

Request Body

The request accepts the following data in JSON format.

arguments

The arguments for the code interpreter. This includes the code to execute and any additional parameters such as the programming language, whether to clear the execution context, and other execution options. The structure of this parameter depends on the specific code interpreter being used.

Type: ToolArguments object

Required: No

name

The name of the code interpreter to invoke.

Type: String

Valid Values: executeCode | executeCommand | readFiles | listFiles | removeFiles | writeFiles | startCommandExecution | getTask | stopTask

Required: Yes

Response Syntax

HTTP/1.1 200 x-amzn-code-interpreter-session-id: sessionId Content-type: application/json { "accessDeniedException": { }, "conflictException": { }, "internalServerException": { }, "resourceNotFoundException": { }, "result": { "content": [ { "data": blob, "description": "string", "mimeType": "string", "name": "string", "resource": { "blob": blob, "mimeType": "string", "text": "string", "type": "string", "uri": "string" }, "size": number, "text": "string", "type": "string", "uri": "string" } ], "isError": boolean, "structuredContent": { "executionTime": number, "exitCode": number, "stderr": "string", "stdout": "string", "taskId": "string", "taskStatus": "string" } }, "serviceQuotaExceededException": { }, "throttlingException": { }, "validationException": { } }

Response Elements

If the action is successful, the service sends back an HTTP 200 response.

The response returns the following HTTP headers.

sessionId

The identifier of the code interpreter session.

Pattern: [0-9a-zA-Z]{1,40}

The following data is returned in JSON format by the service.

accessDeniedException

The exception that occurs when you do not have sufficient permissions to perform an action. Verify that your IAM policy includes the necessary permissions for the operation you are trying to perform.

Type: Exception

HTTP Status Code: 403
conflictException

The exception that occurs when the request conflicts with the current state of the resource. This can happen when trying to modify a resource that is currently being modified by another request, or when trying to create a resource that already exists.

Type: Exception

HTTP Status Code: 409
internalServerException

The exception that occurs when the service encounters an unexpected internal error. This is a temporary condition that will resolve itself with retries. We recommend implementing exponential backoff retry logic in your application.

Type: Exception

HTTP Status Code: 500
resourceNotFoundException

The exception that occurs when the specified resource does not exist. This can happen when using an invalid identifier or when trying to access a resource that has been deleted.

Type: Exception

HTTP Status Code: 404
result

The output produced by executing code in a code interpreter session in Amazon Bedrock. This structure contains the results of code execution, including textual output, structured data, and error information. Agents use these results to generate responses that incorporate computation, data analysis, and visualization.

Type: CodeInterpreterResult object

serviceQuotaExceededException

The exception that occurs when the request would cause a service quota to be exceeded. Review your service quotas and either reduce your request rate or request a quota increase.

Type: Exception

HTTP Status Code: 402
throttlingException

The exception that occurs when the request was denied due to request throttling. This happens when you exceed the allowed request rate for an operation. Reduce the frequency of requests or implement exponential backoff retry logic in your application.

Type: Exception

HTTP Status Code: 429
validationException

The exception that occurs when the input fails to satisfy the constraints specified by the service. Check the error message for details about which input parameter is invalid and correct your request.

Type: Exception

HTTP Status Code: 400

Errors

For information about the errors that are common to all actions, see Common Errors.

AccessDeniedException

The exception that occurs when you do not have sufficient permissions to perform an action. Verify that your IAM policy includes the necessary permissions for the operation you are trying to perform.

HTTP Status Code: 403

ConflictException

The exception that occurs when the request conflicts with the current state of the resource. This can happen when trying to modify a resource that is currently being modified by another request, or when trying to create a resource that already exists.

HTTP Status Code: 409

InternalServerException

The exception that occurs when the service encounters an unexpected internal error. This is a temporary condition that will resolve itself with retries. We recommend implementing exponential backoff retry logic in your application.

HTTP Status Code: 500

ResourceNotFoundException

The exception that occurs when the specified resource does not exist. This can happen when using an invalid identifier or when trying to access a resource that has been deleted.

HTTP Status Code: 404

ServiceQuotaExceededException

The exception that occurs when the request would cause a service quota to be exceeded. Review your service quotas and either reduce your request rate or request a quota increase.

HTTP Status Code: 402

ThrottlingException

The exception that occurs when the request was denied due to request throttling. This happens when you exceed the allowed request rate for an operation. Reduce the frequency of requests or implement exponential backoff retry logic in your application.

HTTP Status Code: 429

ValidationException

The exception that occurs when the input fails to satisfy the constraints specified by the service. Check the error message for details about which input parameter is invalid and correct your request.

HTTP Status Code: 400

See Also

For more information about using this API in one of the language-specific AWS SDKs, see the following: