Skip to content

Dynamodb transact_write_items throws a exception validating ConditionExpression #4053

@LiamCato

Description

@LiamCato

Describe the bug

When using an object supplied from from boto3.dynamodb.conditions import Attr rather than string expression transact_write_items raises an exception:

Unknown parameter in input: "ExpressionAttributeNames", must be one of: TransactItems, ReturnConsumedCapacity, ReturnItemCollectionMetrics, ClientRequestToken

When a ConditionExpression is supplied for a put.

Expected Behavior

I expect the transact_write_items call to function the same where the string representation of a ConditionExpression is supplied or the python object is supplied.

Current Behavior

The string expression works as expected, whereas when using the Attr object, the inject_condition_expressions function unpacks the api_params into:

{'TransactItems': [{'Put': {'TableName': 'my-table', 'Item': {'PK': {'S': 'Test'}, 'SK': {'S': 'Test'}, 'data': {'S': 'Test'}}, 'ConditionExpression': 'attribute_not_exists(#n0)'}}], 'ExpressionAttributeNames': {'#n0': 'SK'}, 'ClientRequestToken': 'c55280d1-da03-4d4f-9096-2cb6b9b2ff97'}

Note how the ExpressionAttributeNames is now outside the item and is validated as part of the top level members which causes the "Unknown parameter in input" exception.

Reproduction Steps

Example:

Works:

from boto3 import resource
dynamodb = resource("dynamodb")
table = dynamodb.Table("mytable")
body = [{'Put': {'TableName': 'my-table-name', 'Item': {'PK': 'Test', 'SK': 'Test', 'data': 'Test'}, 'ConditionExpression': 'attribute_not_exists(SK)' }}]
table.meta.client.transact_write_items(TransactItems=body)

Same action using the Attr object does not work:

from boto3 import resource
from boto3.dynamodb.conditions import Attr
dynamodb = resource("dynamodb")
table = dynamodb.Table("mytable")
body = [{'Put': {'TableName': 'my-table-name', 'Item': {'PK': 'Test', 'SK': 'Test', 'data': 'Test'}, 'ConditionExpression': Attr("SK").not_exists()}}]
table.meta.client.transact_write_items(TransactItems=body)

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.34.65

Environment details (OS name and version, etc.)

Ubuntu 22.04, python 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a confirmed bug.dynamodbneeds-reviewp2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions