Python file produced by the API Playground doesn't include metadata

Looking at the example Python code provided by the Playground after filling out the form for file upload, I noticed that, unlike the other implementations provided, there is no reference to doc_metadata. Of course, since there’s no reference there’s no metadata uploaded with the file.

What am I missing here? This is the code that the Playground provides:

import requests

url = "https://api.vectara.io/v1/upload?c=<customer-id>&o=1&d=true"

payload={}
files=[
  ('file',('file',open('/path/to/file','rb'),'application/octet-stream'))
]
headers = {
  'Content-Type': 'multipart/form-data',
  'Accept': 'application/json',
  'x-api-key': '<api-key>'
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

The headers given cause an error so everything but x-api-key has to be removed. And there’s an empty payload structure.

But no metadata.

When I issue the cURL command that the API Playground produces after providing all of the data, the metadata is properly included with each chunk. So it’s definitely working. All I need is the proper Python version so that I can get past this hurdle.

Hi,

The following python code should work, and set doc_metadata.

def upload_file(customer_id: int, corpus_id: int, file_name: str, file_data: bytes, api_key: str):
    post_headers = {
        "x-api-key": f"{api_key}"
    }
    payload = {
      "doc_metadata": "{\"source\":\"git\",\"url\":\"foo\"}"
    }
    response = requests.post(
        f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}",
        files={"file": (file_name, file_data, "application/octet-stream")},
        verify=True,
        headers=post_headers,
        data=payload)

    if response.status_code != 200:
        print("REST upload failed with code %d, reason %s, text %s",
                response.status_code,
                response.reason,
                response.text)
        return response, False
    return response, True

1 Like

Ah, so it was the empty payload after all! I will give that a try, though I fully expect it to work. Thank you, sir!

It did indeed work! Thanks again, @aamir. I think the playground needs a little tweak in its Python code generation.

Thanks @zigguratt

I will route this to the proper team so that playground gets updated properly.