Zhuoxi2000 opened a new pull request, #916:
URL: https://github.com/apache/flink-agents/pull/916
<!--
* Thank you very much for contributing to Flink Agents.
* Please add the relevant components in the PR title. E.g., [api],
[runtime], [java], [python], [hotfix], etc.
-->
<!-- Please link the PR to the relevant issue(s). Hotfix doesn't need this.
-->
Linked issue: #914
### Purpose of change
<!-- What is the purpose of this change? -->
`AnthropicChatModelConnection.chat` read the response text via
`message.content[0].text`. When the model calls a tool, the response commonly
contains only a `tool_use` block, so `content[0]` is a `ToolUseBlock` and
`.text` raised `AttributeError: 'ToolUseBlock' object has no attribute
'text'`.
Both the `tool_use` and the plain-text return paths used this unsafe access,
even though the code just above already iterates `message.content` filtering
by
`type == "tool_use"` — so the blocks are known to be mixed-type there.
Pick the first text block instead (falling back to `""` when there is none)
and
reuse it in both return paths.
Closes #914
### Tests
<!-- How is this change verified? -->
Added `test_anthropic_response_parsing.py` with unit tests that mock the
client,
so they need no API key and run in the regular unit test job: a tool_use
response with no leading text, one with a leading text block, and a plain
text
response.
`test_tool_use_response_without_leading_text` fails on `main` with
`AttributeError: 'ToolUseBlock' object has no attribute 'text'` and passes
with
this change.
- `pytest flink_agents/integrations/chat_models/anthropic/tests/`: 5 passed,
2 skipped (the existing API-key integration tests).
- `ruff check --no-fix` / `ruff format --check` (pinned 0.11.13): clean.
### API
<!-- Does this change touches any public APIs? -->
No. `chat()` keeps its signature and still returns a `ChatMessage`. Text
responses are unaffected — the first text block is the same value
`content[0]`
used to yield — it only stops crashing when the first block is not text.
### Documentation
<!-- Do not remove this section. Check the proper box only. -->
- [ ] `doc-needed` <!-- Your PR changes impact docs -->
- [x] `doc-not-needed` <!-- Your PR changes do not impact docs -->
- [ ] `doc-included` <!-- Your PR already contains the necessary
documentation updates -->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]