Writing robust Cognite Function using Python SDK

  • 8 June 2023
  • 2 replies
  • 81 views

Userlevel 1
Badge +2

Dear Community,

 

Let me share my experience of writing and deploying a Cognite Function and a lesson what I learnt.

Hope this topic will save someone few hours of troubleshooting.

 

During Data Scientist Basics learning path Python hands-on, I found myself in weird situation when the Cognite Functions I created were perfectly running on my local, but were failing in the sandbox environment after being deployed.

 

Here were the error log:

==================================================

  • 2023-04-27 10:42 Function started
  • The function terminated unexpectedly. Please contact Cognite support if the problem persists.
  • _point.py", line 455, in run_handle
  • result = handle(*function_argument_values)
  • File "/home/site/wwwroot/function/handler.py", line 41, in handle
  • df = client.datapoints.retrieve_dataframe(
  • AttributeError: 'CogniteClient' object has no attribute 'datapoints'
  • 2023-04-27 10:43 Function ended

================================================== 

 

Troubleshooting doc didn’t have the error I was facing https://docs.cognite.com/cdf/functions/known_issues/

 

Thanks to the @roman.chesnokov I got a clue: I was using a previous version of cognite-sdk, while the Cognite sandbox environment the latest one. So I’ve been advised to resolve the issue by either updating the code or using the compatible version of SDK. It turned out that major versions of Python SDK are not backward compatible..

 

Solution. I didn’t want to change the code because tomorrow another new version of SDK may break everything again. So the only way we could protect ourselves from potential future server SDK upgrade is setting the exact SDK version in requirements.txt. This approach will make your function robust and save from future headache. Same goes for the rest of the packages you use. 

 

 

Suggestion. I hope that the Cognite Functions Known Issues documentation could be updated and this case will be added as well.

 

Thank you! 


2 replies

Userlevel 2

Hi, thank you very much for the suggestion, the Python SDK documentation should have this information as well. FYI. the API documentation mention this, but it’s a bit hidden. 

Userlevel 1
Badge +2

Hi @Kristian Gjestad Vangsnes, noted with thanks.

Reply