Skip to content

Commit

Permalink
refactor: add try except logic for JSON parsing and response handling
Browse files Browse the repository at this point in the history
Co-authored-by: Govind Hede <[email protected]>
  • Loading branch information
sfc-gh-twhite and GovindHede committed Dec 6, 2024
1 parent 0558d8f commit 3a917e5
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions agent_gateway/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,32 @@ def _parse_snowflake_response(self, data_str):
# Remove the 'data: ' prefix if it exists
if obj.startswith("data: "):
obj = obj[6:]
# Load the JSON object into a Python dictionary
json_dict = json.loads(obj, strict=False)
# Append the JSON dictionary to the list
json_list.append(json_dict)
try:
# Load the JSON object into a Python dictionary
json_dict = json.loads(obj, strict=False)
# Append the JSON dictionary to the list
json_list.append(json_dict)
except json.JSONDecodeError as e:
gateway_logger.log(
logging.ERROR,
f"Failed to parse JSON object: {obj}. Error: {e}",
)
continue

completion = ""
choices = {}
for chunk in json_list:
choices = chunk["choices"][0]

if "content" in choices["delta"].keys():
completion += choices["delta"]["content"]
# Ensure 'choices' key exists in the chunk
if "choices" in chunk and chunk["choices"]:
choices = chunk["choices"][0]

# Ensure 'delta' key exists in choices and it contains 'content'
if "delta" in choices and "content" in choices["delta"]:
completion += choices["delta"]["content"]
else:
gateway_logger.log(
logging.WARNING, f"Missing or empty 'choices' in chunk: {chunk}"
)

return completion
except KeyError as e:
Expand Down

0 comments on commit 3a917e5

Please sign in to comment.