ibm-cloudibm-watsonwatson-assistantwatson-discovery

Syntax error trying to authenticate / init IBM Watson service in SDK


I am trying to run sample code that uses an IBM Watson service. After setting my username / password or the IAM key as required, the code fails. In Python it is an error like this:

init() got an unexpected keyword argument 'iam_apikey'

What is the reason? What do I need to change?


Solution

  • It seems that you ran into an issue with the Watson SDK. Recently, with the Python SDK V4 and the Node SDK V5, they introduced a breaking change (Python, Node) which I found in their release notes. There is a new, more abstract authentication mechanism that caters to different authentication types. You would need to slightly change the code for how NLC is initialized.

    This is from the Python migration instructions:

    For example, to pass a IAM apikey:

    Before

    from ibm_watson import MyService
    
    service = MyService(
        iam_apikey='{apikey}',
        url='{url}'
    )
    

    After(V4.0)

    from ibm_watson import MyService
    from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
    
    authenticator = IAMAuthenticator('{apikey}')
    service = MyService(
        authenticator=authenticator
    )
    service.set_service_url('{url}')
    

    See some of the IBM Cloud core SDKs for more documentation, e.g., here is the Authentication doc for the core Node SDK.