Python script in Zapier throws 'str' object has no attribute 'copy' error

I’m having trouble with my Python code in Zapier. It keeps giving me this weird error: “Failed to run your Python code ‘str’ object has no attribute ‘copy’”. The strange thing is, it works perfectly fine in VSCode.

Here’s a simplified version of what I’m trying to do:

import requests

# Function to fetch company details

def fetch_company_details(email):
    api_url = f'https://example.com/api/companies?query={email}'
    response = requests.get(api_url, headers={'Authorization': 'Bearer YOUR_TOKEN'})
    if response.ok:
        data = response.json()
        companies = data.get('results', [])
        if companies:
            company_id = companies[0]['id']
            return get_latest_lead(company_id)
    return 'No company found'

# Function to retrieve the latest lead

def get_latest_lead(company_id):
    lead_api = f'https://example.com/api/leads?company={company_id}'
    response = requests.get(lead_api, headers={'Authorization': 'Bearer YOUR_TOKEN'})
    if response.ok:
        leads = response.json().get('leads', [])
        if leads:
            latest_lead = leads[-1]['id']
            return f'https://example.com/leads/{latest_lead}'
    return 'No leads found'

# Getting email input and processing output
email_input = input('Email: ')
result = fetch_company_details(email_input)
output = result
print(output)

I’ve double-checked the Zap settings, confirmed that all field names are matching the expected ones, and simplified the code as much as I could. Yet, the error still persists. Does anyone have any insights on what might be causing this issue in Zapier while it works fine in VSCode?

hey harry, i had similar issues with zapier before. it might be a problem with how zapier handles json data. try converting the response to a dict explicitly:

data = dict(response.json())

also, double-check if your api responses are consistent. sometimes apis return different formats which can cause unexpected errors. hope this helps!

I’ve run into this exact problem with Zapier before. It’s frustrating because the code works fine locally but fails in Zapier’s environment. Here’s what solved it for me:

Zapier’s Python environment can be finicky with JSON handling. Try explicitly converting the response to a dictionary using json.loads():

import json

data = json.loads(response.text)

Also, add some error handling and logging to pinpoint where the issue occurs:

try:
# Your code here
print(f’Debug: data type is {type(data)}‘)
except Exception as e:
print(f’Error: {str(e)}’)
return f’Error occurred: {str(e)}’

This helped me track down where Zapier was misinterpreting the data types. Don’t forget to remove any sensitive info like API keys before deploying. Good luck!

I’ve encountered this issue in Zapier before. The error suggests that somewhere in your code, you’re trying to call the ‘copy’ method on a string object, which isn’t possible. This could happen if Zapier is interpreting one of your JSON responses as a string instead of a dictionary.

To troubleshoot, I’d recommend adding some debug print statements throughout your code to see what types of objects you’re dealing with at each step. You might also want to wrap your JSON parsing in a try-except block to handle potential parsing errors:

'try:
    data = response.json()
except ValueError:
    print(f'Failed to parse JSON: {response.text}')
    return 'Error parsing response'

This will help you identify if the issue is with the API response format. Additionally, consider using response.text instead of response.json() initially, and then manually parse it using json.loads(). This gives you more control over the parsing process.