Python SDK bulk user creation failing with 400 Bad Request on invalid email format

I’ve got a script running to bulk-create users from a CSV file using the Genesys Cloud Python SDK. The goal is straightforward: read the CSV, construct the user object, and push it via the create_user method in a loop.

The script works for the first few rows, but then it starts throwing a 400 Bad Request error. The error message from the SDK is pretty vague, just saying “Validation failed” without pointing to the specific field. I’ve been staring at the CSV and the code for an hour now and can’t spot the issue.

Here’s the relevant part of the loop:

from gen_cloud_sdk_python.models import CreateUserRequest
from gen_cloud_sdk_python.client import ApiClient

# ... auth setup omitted ...

with open('new_users.csv', 'r') as f:
 reader = csv.DictReader(f)
 for row in reader:
 try:
 user_request = CreateUserRequest(
 name=row['Name'],
 email=row['Email'],
 division_id=division_id,
 roles=roles,
 user_type="agent"
 )
 response = api_instance.create_user(body=user_request)
 print(f"Created user: {row['Name']}")
 except Exception as e:
 print(f"Failed to create user {row['Name']}: {e}")

The CSV looks like this:

Name,Email,Role
John Doe,john.doe@example.com,agent
Jane Smith,jane.smith@example.com,agent

I’ve checked the email addresses in the CSV and they all look valid. I’m also using the correct division_id which I fetched earlier in the script. The roles list is populated with the correct role IDs for the “agent” role.

Is there something I’m missing in the CreateUserRequest object? Maybe a required field that’s not obvious? Or is the SDK version I’m using (v132) having issues with the user_type field? I’ve tried removing user_type and it still fails with the same 400 error.

Any help would be appreciated. I’m stuck.

You’re hitting that 400 because the SDK doesn’t always parse the specific validation error from the body when the request fails early. The create_user endpoint is strict about email format compliance with RFC 5322, and it’s likely choking on a trailing space, an uppercase domain, or a missing TLD in that CSV. Don’t trust the CSV data blindly. You need to sanitize the email string before passing it to the CreateUserRequest object. Here’s a quick Python snippet to handle that validation and extraction of the specific error field if it still fails.

import re
from platformclientv2 import ApiClient, UserApi, models

def sanitize_email(email):
 if not email:
 return None
 # Strip whitespace and lowercase
 clean = email.strip().lower()
 # Basic RFC 5322 check
 pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
 if re.match(pattern, clean):
 return clean
 return None

# Inside your loop
api_client = ApiClient()
user_api = UserApi(api_client)

for row in csv_data:
 clean_email = sanitize_email(row['email'])
 if not clean_email:
 print(f"Skipping invalid email: {row['email']}")
 continue

 try:
 user_body = models.CreateUserRequest(
 name=row['name'],
 email=clean_email,
 email_address=clean_email, # Genesys requires this field too
 username=clean_email.split('@')[0]
 )
 user_api.create_user(body=user_body)
 except Exception as e:
 # Log the raw response body to see the specific field error
 print(f"Failed for {clean_email}: {e.body}")

The email_address field is often the culprit if you only set email. Make sure both are populated and match. Check the e.body output for the exact field name Genesys is rejecting.