Skip to content

Agent has error calling tool even if is_enabled = True #2232

Closed as not planned

Description

@NateMoneyLion

Question

  1. I'm using the is_enabled param within the @function_tool decorator to control the tools that are exposed to a user at runtime based on the callable function below to control the value for is_enabled
def _is_enrolled(ctx: RunContextWrapper[AgentContext], agent: AgentBase) -> bool:
    return ctx.context.is_enrolled
  1. I always bump into this error if the opening query for the session already calls a tool e.g. "what's my credit score?".
agents.exceptions.ModelBehaviorError: Tool fetch_data" /> not found in agent Agent

Implementation of tool as follows:

@function_tool(is_enabled=_is_enrolled)
async def fetch_data(
    wrapper: RunContextWrapper[AgentContext],
) -> dict | None:
    """
    High level data points 
    """
    data = await _client._fetch(
          request={
          },
      )

    return data
  1. However, if the opening query is something generic like "hello", this error doesn't bubble up.

Am I doing something wrong here?

Activity

  1. added
    question
    on Dec 24, 2025
  2. changed the title [-]Agent calls tool even if is_enabled = False[/-] [+]Agent has error calling tool even if is_enabled = True[/+] on Dec 24, 2025
  3. ihower commented on Dec 24, 2025

    @ihower
    Contributor

    @NateMoneyLion This happens because the model asks to call fetch_latest_credit_report, but at that time the tool is not registered on the agent due to is_enabled=False. Since the agent no longer has this tool, the SDK raises agents.exceptions.ModelBehaviorError.

    In fact, I can’t reproduce this issue with OpenAI models. If the function schema is not provided, OpenAI models usually do not try to call that function.

    However, other models behave differently. I can reproduce the same issue with Gemini 2.5 Pro: https://gist.github.com/ihower/39435370e337c03c0ef92dffd42f9af4

    So this looks like a model behavior issue. The model seems to remember that the function appeared earlier in the conversation and assumes it is always available, even when it is no longer exposed.

    You can try switching to a different model, or adjusting your prompt to make the model avoid calling functions that may not exist.

  4. NateMoneyLion commented on Dec 26, 2025

    @NateMoneyLion
    Author

    oooh, I'll have a try then.

    One thing to note, I did a bit of logging and saw that the fetch _latest_credit_report tool was available

    Available tools: ['fetch_latest_credit_report', ...]
    

    However, in the error log, it outputs it as

    agents.exceptions.ModelBehaviorError: Tool fetch_latest_credit_report" /> not found in agent Credit Monitoring Agent
    

    Note that the tool name in the error is fetch_latest_credit_report " />. I don't know if this is how the error log should look or if there is a corruption in name-parsing

  5. ihower commented on Dec 26, 2025

    @ihower
    Contributor

    @NateMoneyLion The SDK does not change the tool name.

    This is likely because the model you are using does not support function calling very well, so it returned this incorrect name.

  6. NateMoneyLion commented on Dec 26, 2025

    @NateMoneyLion
    Author

    I've been testing out Claude Sonnet (jumping between 4 and 4.5) so I don't know if this is an edge case.
    Oddly, after trying a lot more, oddly, the tool error comes and goes even with the same query.

    I'll try with the OpenAI models.

  7. ihower commented on Dec 26, 2025

    @ihower
    Contributor

    @NateMoneyLion If you’re using Claude Sonnet, it shouldn’t return a malformed tool name. If you can share a minimal reproducible code, I can help take another look.

  8. NateMoneyLion commented on Dec 29, 2025

    @NateMoneyLion
    Author

    @ihower ironically, it is the code that you've tested with for Gemini. Let me just paste my exact implementation

    <redacted>
    

    I've omitted any message history saving logic at the moment

  9. ihower commented on Jan 5, 2026

    @ihower
    Contributor

    @NateMoneyLion One thing I noticed that may be related: you are actually using OpenAIChatCompletionsModel, not LiteLLMModel.

    OpenAIChatCompletionsModel uses Claude’s Chat Completions API (https://platform.claude.com/docs/en/api/openai-sdk). And this API does not return thought data. Because of that, the model cannot receive reasoning tokens during the same turn of function calling. This may make function calling behave less reliably.

  10. NateMoneyLion commented on Jan 6, 2026

    @NateMoneyLion
    Author

    ahhh, that didn't come across my mind. Thanks for the heads up, I'll apply that change too.

  11. github-actions commented on Jan 14, 2026

    @github-actions
    Contributor

    This issue is stale because it has been open for 7 days with no activity.

  12. github-actions commented on Jan 18, 2026

    @github-actions
    Contributor

    This issue was closed because it has been inactive for 3 days since being marked as stale.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions