-
Notifications
You must be signed in to change notification settings - Fork 29
WIP tmp #409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mjnovice
wants to merge
1
commit into
main
Choose a base branch
from
mj/tmp-tool-call
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP tmp #409
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -54,6 +54,8 @@ def create_llm_node( | |
| tool_choice_required_value = _get_required_tool_choice_by_model(model) | ||
|
|
||
| async def llm_node(state: AgentGraphState): | ||
| from .router import filter_control_flow_tool_calls_from_state | ||
|
|
||
| messages: list[AnyMessage] = state.messages | ||
|
|
||
| consecutive_thinking_messages = count_consecutive_thinking_messages(messages) | ||
|
|
@@ -69,6 +71,16 @@ async def llm_node(state: AgentGraphState): | |
| f"LLM returned {type(response).__name__} instead of AIMessage" | ||
| ) | ||
|
|
||
| return {"messages": [response]} | ||
| # Create temporary state with the response to filter tool calls | ||
| temp_state = AgentGraphState( | ||
| messages=[*messages, response], | ||
| inner_state=state.inner_state, | ||
| ) | ||
|
Comment on lines
+74
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the temp state? |
||
|
|
||
| # Filter control flow tool calls from the AIMessage | ||
| filtered_state = filter_control_flow_tool_calls_from_state(temp_state) | ||
|
|
||
| # Return only the (possibly filtered) last message | ||
| return {"messages": [filtered_state.messages[-1]]} | ||
|
|
||
| return llm_node | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,78 @@ def __filter_control_flow_tool_calls( | |
| return [tc for tc in tool_calls if tc.get("name") not in FLOW_CONTROL_TOOLS] | ||
|
|
||
|
|
||
| def filter_control_flow_tool_calls_from_state(state: AgentGraphState) -> AgentGraphState: | ||
| """Remove filtered control flow tool calls from AIMessage to prevent OpenAI API errors. | ||
|
|
||
| When multiple tools are called and one is a control flow tool (end_execution, raise_error), | ||
| the control flow tools are filtered out for execution. However, the AIMessage still | ||
| contains these tool calls, causing OpenAI to expect ToolMessages for them. | ||
|
|
||
| This node updates the AIMessage to only include tool calls that will actually be executed. | ||
| """ | ||
| messages = state.messages | ||
| if not messages: | ||
| return state | ||
|
|
||
| last_message = messages[-1] | ||
| if not isinstance(last_message, AIMessage) or not last_message.tool_calls: | ||
| return state | ||
|
|
||
| original_tool_calls = list(last_message.tool_calls) | ||
| if len(original_tool_calls) <= 1: | ||
| # No filtering needed for single tool calls | ||
| return state | ||
|
|
||
| # Check if any control flow tools would be filtered | ||
| has_control_flow = any( | ||
| tc.get("name") in FLOW_CONTROL_TOOLS for tc in original_tool_calls | ||
| ) | ||
| if not has_control_flow: | ||
| # No control flow tools to filter | ||
| return state | ||
|
|
||
| # Filter out control flow tools | ||
| filtered_tool_calls = [ | ||
| tc for tc in original_tool_calls if tc.get("name") not in FLOW_CONTROL_TOOLS | ||
| ] | ||
|
|
||
| if len(filtered_tool_calls) == len(original_tool_calls): | ||
| # No filtering occurred | ||
| return state | ||
|
|
||
| # Filter content if it's a list of tool call dicts | ||
| filtered_content = last_message.content | ||
| if isinstance(last_message.content, list): | ||
| # Filter out control flow tools from content as well | ||
| filtered_ids = {tc["id"] for tc in filtered_tool_calls} | ||
| filtered_content = [ | ||
| item | ||
| for item in last_message.content | ||
| if not ( | ||
| isinstance(item, dict) | ||
| and item.get("type") == "function_call" | ||
| and item.get("call_id") not in filtered_ids | ||
| ) | ||
| ] | ||
|
Comment on lines
+66
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this only works for OpenAPI |
||
|
|
||
| # Create new AIMessage with only non-control-flow tool calls | ||
| updated_message = AIMessage( | ||
| content=filtered_content, | ||
| tool_calls=filtered_tool_calls, | ||
| id=last_message.id, | ||
| additional_kwargs=last_message.additional_kwargs, | ||
| response_metadata=last_message.response_metadata, | ||
| usage_metadata=last_message.usage_metadata, | ||
| ) | ||
|
|
||
| # Return updated state with modified message | ||
| updated_messages = list(messages[:-1]) + [updated_message] | ||
| return AgentGraphState( | ||
| messages=updated_messages, | ||
| inner_state=state.inner_state, | ||
| ) | ||
|
|
||
|
|
||
| def __has_control_flow_tool(tool_calls: list[ToolCall]) -> bool: | ||
| """Check if any tool call is of a control flow tool.""" | ||
| return any(tc.get("name") in FLOW_CONTROL_TOOLS for tc in tool_calls) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally not the biggest fan of scoped imports, but that's just me.
I'd also move that utility function somewhere else, since llm_node importing from router feels weird if you think about separating concerns.