advanced-stack / py-llm-core

A pythonic library providing light-weighted interface with LLMs
MIT License
103 stars 8 forks source link

should the exception be caught in deserialize? #14

Open ShaneJIan opened 3 months ago

ShaneJIan commented 3 months ago

BaseParser.java def deserialize(self, json_str): try: attributes = dirtyjson.loads(json_str) return from_dict(self.target_cls, attributes) except dirtyjson.error.Error as e: print(f"Failed to parse JSON string: {e}")

handle the error appropriately, perhaps returning None or a default value

    return None
paschembri commented 3 months ago

I thought of raising a custom exception when the result is not valid JSON.

I don't think we should return None or a default value because the goal is to actually create an instance of target_cls.

If the data is not a JSON string, our only choice is to fail and let the upstream application deal with that case (retry, change the context window, ...)

Does a custom exception make sense, something like InvalidLLMJSONResponse ?