Create_index is failing with *TypeError: expected string or bytes-like object*

I am following the pinecone-io examples on github (link)
This too fails:
pinecone.create_index(“example-index”, dimension=1024)

TypeError Traceback (most recent call last)
Cell In[137], line 1
----> 1 pinecone.create_index(“example-index”, dimension=1024)

File ~/.local/lib/python3.10/site-packages/pinecone/manage.py:118, in create_index(name, dimension, timeout, index_type, metric, replicas, shards, pods, pod_type, index_config, metadata_config, source_collection)
82 “”“Creates a Pinecone index.
83
84 :param name: the name of the index.
(…)
114 :param timeout: Timeout for wait until index gets ready. If None, wait indefinitely; if >=0, time out after this many seconds; if -1, return immediately and do not wait. Default: None
115 “””
116 api_instance = _get_api_instance()
→ 118 api_instance.create_index(create_request=CreateRequest(
119 name=name,
120 dimension=dimension,
121 index_type=index_type,
122 metric=metric,
123 replicas=replicas,
124 shards=shards,
125 pods=pods,
126 pod_type=pod_type,
127 index_config=index_config or {},
128 metadata_config=metadata_config,
129 source_collection=source_collection
130 ))
132 def is_ready():
133 status = _get_status(name)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api_client.py:776, in Endpoint.call(self, *args, **kwargs)
765 def call(self, *args, **kwargs):
766 “”" This method is invoked when endpoints are called
767 Example:
768
(…)
774
775 “”"
→ 776 return self.callable(self, *args, **kwargs)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api/index_operations_api.py:370, in IndexOperationsApi.init..__create_index(self, **kwargs)
366 kwargs[‘_check_return_type’] = kwargs.get(
367 ‘_check_return_type’, True
368 )
369 kwargs[‘_host_index’] = kwargs.get(‘_host_index’)
→ 370 return self.call_with_http_info(**kwargs)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api_client.py:838, in Endpoint.call_with_http_info(self, **kwargs)
834 header_list = self.api_client.select_header_content_type(
835 content_type_headers_list)
836 params[‘header’][‘Content-Type’] = header_list
→ 838 return self.api_client.call_api(
839 self.settings[‘endpoint_path’], self.settings[‘http_method’],
840 params[‘path’],
841 params[‘query’],
842 params[‘header’],
843 body=params[‘body’],
844 post_params=params[‘form’],
845 files=params[‘file’],
846 response_type=self.settings[‘response_type’],
847 auth_settings=self.settings[‘auth’],
848 async_req=kwargs[‘async_req’],
849 _check_type=kwargs[‘_check_return_type’],
850 _return_http_data_only=kwargs[‘_return_http_data_only’],
851 _preload_content=kwargs[‘_preload_content’],
852 _request_timeout=kwargs[‘_request_timeout’],
853 _host=_host,
854 collection_formats=params[‘collection_format’])

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api_client.py:413, in ApiClient.call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, async_req, _return_http_data_only, collection_formats, _preload_content, _request_timeout, _host, _check_type)
359 “”“Makes the HTTP request (synchronous) and returns deserialized data.
360
361 To make an async_req request, set the async_req parameter.
(…)
410 then the method will return the response directly.
411 “””
412 if not async_req:
→ 413 return self.__call_api(resource_path, method,
414 path_params, query_params, header_params,
415 body, post_params, files,
416 response_type, auth_settings,
417 _return_http_data_only, collection_formats,
418 _preload_content, _request_timeout, _host,
419 _check_type)
421 return self.pool.apply_async(self.__call_api, (resource_path,
422 method, path_params,
423 query_params,
(…)
431 _request_timeout,
432 _host, _check_type))

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api_client.py:200, in ApiClient.__call_api(self, resource_path, method, path_params, query_params, header_params, body, post_params, files, response_type, auth_settings, _return_http_data_only, collection_formats, _preload_content, _request_timeout, _host, _check_type)
196 url = _host + resource_path
198 try:
199 # perform request and return response
→ 200 response_data = self.request(
201 method, url, query_params=query_params, headers=header_params,
202 post_params=post_params, body=body,
203 _preload_content=_preload_content,
204 _request_timeout=_request_timeout)
205 except ApiException as e:
206 e.body = e.body.decode(‘utf-8’)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/api_client.py:459, in ApiClient.request(self, method, url, query_params, headers, post_params, body, _preload_content, _request_timeout)
451 return self.rest_client.OPTIONS(url,
452 query_params=query_params,
453 headers=headers,
(…)
456 _request_timeout=_request_timeout,
457 body=body)
458 elif method == “POST”:
→ 459 return self.rest_client.POST(url,
460 query_params=query_params,
461 headers=headers,
462 post_params=post_params,
463 _preload_content=_preload_content,
464 _request_timeout=_request_timeout,
465 body=body)
466 elif method == “PUT”:
467 return self.rest_client.PUT(url,
468 query_params=query_params,
469 headers=headers,
(…)
472 _request_timeout=_request_timeout,
473 body=body)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/rest.py:271, in RESTClientObject.POST(self, url, headers, query_params, post_params, body, _preload_content, _request_timeout)
269 def POST(self, url, headers=None, query_params=None, post_params=None,
270 body=None, _preload_content=True, _request_timeout=None):
→ 271 return self.request(“POST”, url,
272 headers=headers,
273 query_params=query_params,
274 post_params=post_params,
275 _preload_content=_preload_content,
276 _request_timeout=_request_timeout,
277 body=body)

File ~/.local/lib/python3.10/site-packages/pinecone/core/client/rest.py:157, in RESTClientObject.request(self, method, url, query_params, headers, body, post_params, _preload_content, _request_timeout)
155 if body is not None:
156 request_body = json.dumps(body)
→ 157 r = self.pool_manager.request(
158 method, url,
159 body=request_body,
160 preload_content=_preload_content,
161 timeout=timeout,
162 headers=headers)
163 elif headers[‘Content-Type’] == ‘application/x-www-form-urlencoded’: # noqa: E501
164 r = self.pool_manager.request(
165 method, url,
166 fields=post_params,
(…)
169 timeout=timeout,
170 headers=headers)

File /usr/lib/python3/dist-packages/urllib3/request.py:78, in RequestMethods.request(self, method, url, fields, headers, **urlopen_kw)
74 return self.request_encode_url(
75 method, url, fields=fields, headers=headers, **urlopen_kw
76 )
77 else:
—> 78 return self.request_encode_body(
79 method, url, fields=fields, headers=headers, **urlopen_kw
80 )

File /usr/lib/python3/dist-packages/urllib3/request.py:170, in RequestMethods.request_encode_body(self, method, url, fields, headers, encode_multipart, multipart_boundary, **urlopen_kw)
167 extra_kw[“headers”].update(headers)
168 extra_kw.update(urlopen_kw)
→ 170 return self.urlopen(method, url, **extra_kw)

File /usr/lib/python3/dist-packages/urllib3/poolmanager.py:375, in PoolManager.urlopen(self, method, url, redirect, **kw)
373 response = conn.urlopen(method, url, **kw)
374 else:
→ 375 response = conn.urlopen(method, u.request_uri, **kw)
377 redirect_location = redirect and response.get_redirect_location()
378 if not redirect_location:

File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:699, in HTTPConnectionPool.urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
696 self._prepare_proxy(conn)
698 # Make the request on the httplib connection object.
→ 699 httplib_response = self._make_request(
700 conn,
701 method,
702 url,
703 timeout=timeout_obj,
704 body=body,
705 headers=headers,
706 chunked=chunked,
707 )
709 # If we’re going to release the connection in finally:, then
710 # the response doesn’t need to know about the connection. Otherwise
711 # it will also try to release it and we’ll have a double-release
712 # mess.
713 response_conn = conn if not release_conn else None

File /usr/lib/python3/dist-packages/urllib3/connectionpool.py:394, in HTTPConnectionPool._make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
392 conn.request_chunked(method, url, **httplib_request_kw)
393 else:
→ 394 conn.request(method, url, **httplib_request_kw)
396 # We are swallowing BrokenPipeError (errno.EPIPE) since the server is
397 # legitimately able to close the connection after sending a valid response.
398 # With this behaviour, the received response is still readable.
399 except BrokenPipeError:
400 # Python 3

File /usr/lib/python3/dist-packages/urllib3/connection.py:234, in HTTPConnection.request(self, method, url, body, headers)
232 if “user-agent” not in (six.ensure_str(k.lower()) for k in headers):
233 headers[“User-Agent”] = _get_default_user_agent()
→ 234 super(HTTPConnection, self).request(method, url, body=body, headers=headers)

File /usr/lib/python3.10/http/client.py:1282, in HTTPConnection.request(self, method, url, body, headers, encode_chunked)
1279 def request(self, method, url, body=None, headers={}, *,
1280 encode_chunked=False):
1281 “”“Send a complete request to the server.”“”
→ 1282 self._send_request(method, url, body, headers, encode_chunked)

File /usr/lib/python3.10/http/client.py:1323, in HTTPConnection._send_request(self, method, url, body, headers, encode_chunked)
1320 encode_chunked = False
1322 for hdr, value in headers.items():
→ 1323 self.putheader(hdr, value)
1324 if isinstance(body, str):
1325 # RFC 2616 Section 3.7.1 says that text default has a
1326 # default charset of iso-8859-1.
1327 body = _encode(body, ‘body’)

File /usr/lib/python3/dist-packages/urllib3/connection.py:219, in HTTPConnection.putheader(self, header, *values)
217 “”" “”"
218 if not any(isinstance(v, str) and v == SKIP_HEADER for v in values):
→ 219 _HTTPConnection.putheader(self, header, *values)
220 elif six.ensure_str(header.lower()) not in SKIPPABLE_HEADERS:
221 raise ValueError(
222 “urllib3.util.SKIP_HEADER only supports ‘%s’”
223 % (“', '”.join(map(str.title, sorted(SKIPPABLE_HEADERS))),)
224 )

File /usr/lib/python3.10/http/client.py:1259, in HTTPConnection.putheader(self, header, *values)
1256 elif isinstance(one_value, int):
1257 values[i] = str(one_value).encode(‘ascii’)
→ 1259 if _is_illegal_header_value(values[i]):
1260 raise ValueError(‘Invalid header value %r’ % (values[i],))
1262 value = b’\r\n\t’.join(values)

TypeError: expected string or bytes-like object

Hi @dilip.panicker. Before creating your index be sure you’ve initialized your connection to your Pinecone project.

pinecone.init(api_key=API_KEY,environment=ENVIRONMENT)

I have the same error message, and I did do:

pinecone.init(api_key=API_KEY,environment=ENVIRONMENT)

Could you help?

I am getting the same error. I also did the pinecone.init(api_key=API_KEY,environment=ENVIRONMENT). Any help?

@dipra.chakraborty could you share your full stacktrace so we can see what’s happening? Also, make sure you’re running the latest version of the client. 2.2.4 is available on PyPi, so just run pip install -U pinecone-client to make sure you’re updated.

Also, please share the code you’re using to initialize the connection. Be sure not to include any API keys.