title: Using CombinePerKey in Google Cloud Dataflow Python
body: <p>I'm trying to run a simple Dataflow Python pipeline that gets certain user events from BigQuery and produces a per-user event count. </p>
  
  <pre><code>p = df.Pipeline(argv=pipeline_args)
  result_query = "..."
  data = p | df.io.Read(df.io.BigQuerySource(query=result_query))
  user_events = data|df.Map(lambda x: (x['users_user_id'], 1))
  user_event_counts = user_events|df.CombinePerKey(sum)
  </code></pre>
  
  <p>Running this gives me an error:</p>
  
  <pre><code>TypeError: Expected tuple, got int [while running 'Map(&lt;lambda at user_stats.py:...&gt;)']
  </code></pre>
  
  <p>Data before the <code>CombinePerKey</code> transform is in this form:</p>
  
  <pre><code>(u'55107178236374', 1)
  (u'55107178236374', 1)
  (u'55107178236374', 1)
  (u'2296845644499670', 1)
  (u'2296845644499670', 1)
  (u'1489727796186326', 1)
  (u'1489727796186326', 1)
  (u'1489727796186326', 1)
  (u'1489727796186326', 1)
  </code></pre>
  
  <p>If instead calculate <code>user_event_counts</code> with this:</p>
  
  <pre><code>user_event_counts = (user_events|df.GroupByKey()|
      df.Map('count', lambda (user, ones): (user, sum(ones))))
  </code></pre>
  
  <p>then there are no errors and I get the result I expect. </p>
  
  <p>Based the <a href="https://cloud.google.com/dataflow/model/combine#per-key" rel="nofollow">docs</a> I would have expected similar behaviour from both approaches. I obviously missing something with respect to <code>CombinePerKey</code> but I can't see what it is. Any tips appreciated!</p>