fix: Handle api key error (#488)

* Propagate exception up to task coroutine instead of breaking the for loop manually, await coroutine so exception is returned to caller

* Remove redundant traceback as is already called in step function lower on the stack

* Change message as error could occur in middle of task also

* Fix linter errors

* Raise exception only on api key error

* Add TODO
This commit is contained in:
George Balch
2024-04-01 19:49:06 -07:00
committed by GitHub
parent fa40d379de
commit a9f469f0e7
2 changed files with 8 additions and 3 deletions

View File

@@ -63,8 +63,7 @@ class AgentController:
finished = await self.step(i)
except Exception as e:
print("Error in loop", e, flush=True)
traceback.print_exc()
break
raise e
if finished:
break
if not finished:
@@ -94,6 +93,9 @@ class AgentController:
observation = AgentErrorObservation(str(e))
print_with_indent("\nAGENT ERROR:\n%s" % observation)
traceback.print_exc()
# TODO Change to more robust error handling
if "The api_key client option must be set" in observation.content:
raise
self.update_state_after_step()
await self._run_callbacks(action)

View File

@@ -156,7 +156,10 @@ class Session:
if self.controller is None:
await self.send_error("No agent started. Please wait a second...")
return
self.agent_task = asyncio.create_task(self.controller.start_loop(task), name="agent loop")
try:
self.agent_task = await asyncio.create_task(self.controller.start_loop(task), name="agent loop")
except Exception:
await self.send_error("Error during task loop.")
def on_agent_event(self, event: Observation | Action):
"""Callback function for agent events.