AWQL - How to properly use a list as predicates values (Python)?

570 views
Skip to first unread message

Matthias Baader

unread,
Jun 14, 2018, 6:03:19 AM6/14/18
to AdWords API and Google Ads API Forum
Hi,

how can I assign a list to predicates values in an AWQL query string?

this code works:
query = (adwords.ServiceQueryBuilder()
.Select('Id', 'Status')
.Where('BaseCampaignId').In("1435278870", "2345678901", "3456789012")
.Build())


this code works too:
query = (adwords.ServiceQueryBuilder()
.Select('Id', 'Status')
.Where('BaseCampaignId').In(1435278870, 2345678901, 3456789012)
.Build())

and even this code works:
campagin_ids = [1435278870, 2345678901, 3456789012]           
query = (adwords.ServiceQueryBuilder()
.Select('Id', 'Status')
.Where('BaseCampaignId').In(campaign_ids[0],campaign_ids[1], campaign_ids[2])         
.Build())                      

unfortunately, this code throws an error:
campagin_ids = [1435278870, 2345678901, 3456789012]           
query = (adwords.ServiceQueryBuilder()
.Select('Id', 'Status')
.Where('BaseCampaignId').In(campaign_ids)         
.Build())

> googleads.errors.GoogleAdsServerFault: [QueryError{reason=INVALID_WHERE_CLAUSE, message=}]


So, how do I have to handle the list to make it work properly as predicates values (without having to assign the values manually)? 

Best regards
Matthias


Teja Makani

unread,
Jun 14, 2018, 3:19:52 PM6/14/18
to AdWords API and Google Ads API Forum
Hello Matthias,

From API perspective, the ".Where().In()" clause is expected to have the list of values like XXXXXX, XXXXXXXX, XXXXXXX but when you pass a list, looks like it is replacing with extra square brackets "[ ]" causing this error as it is not conforming to AWQL syntax. You will need to trim the brackets or use a for loop to read from a list and pass campaignIds one by one. 

Regards,
Sai Teja, AdWords API Team.

Matthias Baader

unread,
Jun 15, 2018, 5:40:41 AM6/15/18
to AdWords API and Google Ads API Forum
Hi Sai Teja,

thank you for your answer, which unfortunately didn't help me at all.
After many hours of trial and error (and I tried everything like str(), rep(), tuple(), for ... inside the .In() or in seperate variables or defs), I finally found the solution. It's called "unpacking argument-lists" in Python and "splat" in Ruby or Perl:

This is what the correct code looks like:
campagin_ids = [1435278870, 2345678901, 3456789012]          
query
= (adwords.ServiceQueryBuilder()
 
.Select('Id', 'Status')

 
.Where('BaseCampaignId').In(*campaign_ids)#Pay attention to the asterisk "*" in front of the object!        
 
.Build())


To be honest, I wonder why you couldn't call this solution. This is certainly a problem that everyone will encounter sooner or later if they want to use the AdWords-API. In my opinion, this should be prominently featured in the AWQL documentation.

Best regads
Matthias



It's hard to believe that there is no

Teja Makani

unread,
Jun 15, 2018, 2:42:02 PM6/15/18
to AdWords API and Google Ads API Forum
Hello Matthias, 

Glad that you were able to resolve the issue. I will share your feedback with the team. 

ishan kumar

unread,
Jul 8, 2019, 2:39:49 PM7/8/19
to AdWords API and Google Ads API Forum
Thanks.. It also works for me
Reply all
Reply to author
Forward
0 new messages