Skip to content

Handling LDP Agent incorrectly giving Message#1299

Merged
jamesbraza merged 3 commits intomainfrom
crash-tool-calls
Feb 22, 2026
Merged

Handling LDP Agent incorrectly giving Message#1299
jamesbraza merged 3 commits intomainfrom
crash-tool-calls

Conversation

@jamesbraza
Copy link
Copy Markdown
Collaborator

We are seeing crashes due to GPT 5.2 occasionally giving a Message over a ToolRequestMessage. Arguably we should fix this in LDP, but for now we can react here too:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.13/site-packages/ldp/alg/rollout.py", line 61, in reraise_exc_as
    yield
  File "/app/.venv/lib/python3.13/site-packages/ldp/alg/rollout.py", line 453, in _take_step
    next_obs, reward, done, trunc = await env.step(action.value)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/paperqa_server/agents/env.py", line 1100, in step
    messages, reward, done, truncated = await super().step(action)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/aviary/envs/labbench/env.py", line 146, in step
    messages, reward, done, truncated = await super().step(action)
                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/paperqa/agents/env.py", line 312, in step
    self.state.record_action(action)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/paperqa/agents/tools.py", line 85, in record_action
    self.session.tool_history.append([tc.function.name for tc in action.tool_calls])
                                                                 ^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.13/site-packages/pydantic/main.py", line 1026, in __getattr__
    raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'Message' object has no attribute 'tool_calls'

@jamesbraza jamesbraza self-assigned this Feb 20, 2026
@jamesbraza jamesbraza added the bug Something isn't working label Feb 20, 2026
Copilot AI review requested due to automatic review settings February 20, 2026 23:52
@dosubot dosubot bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Feb 20, 2026
@dosubot
Copy link
Copy Markdown

dosubot bot commented Feb 20, 2026

Related Documentation

Checked 1 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a critical bug where GPT 5.2 occasionally returns a plain Message object instead of the expected ToolRequestMessage, causing crashes when the code tries to access the tool_calls attribute. The fix implements defensive programming by handling both message types gracefully in the record_action method and the environment's step method.

Changes:

  • Modified record_action method signature to accept both Message and ToolRequestMessage types
  • Added runtime type checking in record_action to only append tool history when a ToolRequestMessage is received
  • Added type guard in environment's step method to return a helpful error message when receiving a plain Message

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/paperqa/agents/tools.py Updated record_action signature to handle both Message and ToolRequestMessage, with conditional tool history tracking
src/paperqa/agents/env.py Added type guard in step method to gracefully handle plain Message objects and return error response

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@jamesbraza jamesbraza force-pushed the crash-tool-calls branch 2 times, most recently from 39b2f20 to 1a1790a Compare February 21, 2026 00:59
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 21, 2026
@jamesbraza jamesbraza force-pushed the crash-tool-calls branch 2 times, most recently from 915ecf6 to fb232e5 Compare February 21, 2026 02:04
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Feb 21, 2026
@jamesbraza jamesbraza force-pushed the crash-tool-calls branch 2 times, most recently from b81a2a6 to f3bda5f Compare February 21, 2026 02:06
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Feb 21, 2026
@jamesbraza jamesbraza merged commit c0d1bd3 into main Feb 22, 2026
7 checks passed
@jamesbraza jamesbraza deleted the crash-tool-calls branch February 22, 2026 21:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants