Skip to content

Python: Agent(client=FoundryChatClient(...)) type-checks as invalid with ty, runtime works #6938

Closed

Description

@pamelafox

Description

Agent(client=FoundryChatClient(...)) works at runtime in hosted samples, but ty reports a type mismatch.

In our deployed sample this currently requires an inline ignore on the client=... argument.

This looks similar in spirit to prior static-type mismatches (for example #560), but this case is specifically about FoundryChatClient not satisfying the expected Agent client protocol in ty diagnostics.

Minimal repro

from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import DefaultAzureCredential

client = FoundryChatClient(
    project_endpoint="https://example.services.ai.azure.com/api/projects/example",
    model="gpt-5.2",
    credential=DefaultAzureCredential(),
)

agent = Agent(
    client=client,
    instructions="You are a friendly assistant",
)

Observed ty diagnostic

error[invalid-argument-type]: Argument to bound method `__init__` is incorrect
Expected `SupportsChatGetResponse[...]`, found `FoundryChatClient[...]`

Runtime behavior

The same pattern runs successfully in hosted agent code paths; this appears to be a typing/protocol mismatch rather than a runtime incompatibility.

Environment

  • OS: macOS
  • Python: 3.13.13
  • ty: 0.0.21

Relevant package versions

From sample agents/pyproject.toml:

  • agent-framework-core>=1.10.0
  • agent-framework-openai>=1.10.0
  • agent-framework-foundry>=1.10.0
  • agent-framework-foundry-hosting>=1.0.0a260630
  • agent-framework-orchestrations>=1.0.0

Request

Could you confirm whether this is an expected typing limitation with FoundryChatClient + Agent, or a bug in type annotations/protocol definitions? If it is a bug, a typing fix would help remove ignores from hosted sample code.

Activity

  1. added
    pythonUsage: [Issues, PRs], Target: Python
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Jul 6, 2026
  2. added
    agentsUsage: [Issues, PRs], Target: Single agent
    and removed
    triageUsage: [Issues], Target: All issues that still need to be triaged
    on Jul 7, 2026
  3. github-actions commented on Jul 7, 2026

    @github-actions
    Contributor

    👋 Hi Pamela Fox (@pamelafox) — thanks for the report!

    Your reported ty error (error[invalid-argument-type]: Expected SupportsChatGetResponse[...], found FoundryChatClient[...]) does not reproduce with ty 0.0.21 (your stated version) or ty 0.0.56 against the current v1.10.0 codebase — both pass cleanly on your exact repro snippet. To investigate further we would need: (1) your ty project configuration (any ty.toml, pyproject.toml [tool.ty] settings, or CLI flags you use), (2) the output of pip show agent-framework-core agent-framework-foundry confirming exact installed versions, and (3) whether ty is resolving types from your venv or from source. It's possible your environment is missing the .pyi stub at agent_framework/foundry/__init__.pyi or ty is picking up a different package installation.

    Once you can share the details above, we'll take another look. 🙏

  4. eavanvalkenburg commented on Jul 7, 2026

    @eavanvalkenburg
    Member

    Hi Pamela Fox (@pamelafox)
    in this line, we specifically check for this:

    assert isinstance(client, SupportsChatGetResponse)

    and when I run ty (v 0.0.46) against the test file everything passes.

    and when I add:

    client = FoundryChatClient(project_client=mock_project, model="test-model")
    
    agent = Agent(client=client, instructions="test agent")

    as a test, it also passes ty. When I run with ty==0.0.21 I also get passing checks. I am looking further, but if you have additional info, let me know!

  5. pamelafox commented on Jul 7, 2026

    @pamelafox
    MemberAuthor

    Hm, the full file is here:
    https://github.com/Azure-Samples/foundry-hosted-agentframework-demos/blob/main/agents/stage4_foundry_hosted.py

    I get the error both from the ty VS Code extension and my local ty, across all the versions (I just upgraded as well to check 0.56)

    error[invalid-argument-type]: Argument to `Agent.__init__` is incorrect
      --> agents/stage4_foundry_hosted.py:99:9
       |
    99 |         client=client,
       |         ^^^^^^^^^^^^^ Expected `SupportsChatGetResponse[OpenAIChatOptions[None] | (ChatOptions[None] & ChatOptions[Any]) | None | dict[str, bool]]`, found `FoundryChatClient[OpenAIChatOptions[None]]`
       |
    info: type `FoundryChatClient[OpenAIChatOptions[None]]` is not assignable to protocol `SupportsChatGetResponse[OpenAIChatOptions[None] | (ChatOptions[None] & ChatOptions[Any]) | None | dict[str, bool]]`
    info: └── protocol member `get_response` is incompatible
    info: Method defined here
        --> agents/.venv/lib/python3.14/site-packages/agent_framework/_agents.py:1730:9
         |
    1730 |     def __init__(
         |         ^^^^^^^^
    1731 |         self,
    1732 |         client: SupportsChatGetResponse[OptionsCoT],
         |         ------------------------------------------- Parameter declared here
         |
    
  6. pamelafox commented on Oct 19, 2026

    @pamelafox
    MemberAuthor

    I found what triggers this in my code: passing a plain dict as default_options.

    Agent.__init__ uses one type variable for both arguments:

    client: SupportsChatGetResponse[OptionsCoT],
    default_options: OptionsCoT | None = None,

    When default_options is a dict literal such as {"store": False}, ty includes dict[str, bool] in the solution for OptionsCoT, and then FoundryChatClient[OpenAIChatOptions[None]] no longer matches the expected client type. The dict[str, bool] shows up at the end of the expected type in the error:

    Expected `SupportsChatGetResponse[OpenAIChatOptions[None] | (ChatOptions[None] & ChatOptions[Any]) | None | dict[str, bool]]`, found `FoundryChatClient[OpenAIChatOptions[None]]`
    

    That would explain why the snippet in the original report didn't reproduce: it doesn't pass default_options.

    Code that shows the error

    from agent_framework import Agent
    from agent_framework.foundry import FoundryChatClient
    from azure.identity import DefaultAzureCredential
    
    client = FoundryChatClient(
        project_endpoint="https://example.services.ai.azure.com/api/projects/example",
        model="gpt-5.5",
        credential=DefaultAzureCredential(),
    )
    
    agent = Agent(
        client=client,
        instructions="You are a friendly assistant",
        default_options={"store": False},  # error is reported on `client=` above
    )

    Workaround

    Passing a typed options object fixes the error, and it's still a plain dict at runtime:

    from agent_framework.openai import OpenAIChatOptions
    
    agent = Agent(
        client=client,
        instructions="You are a friendly assistant",
        default_options=OpenAIChatOptions(store=False),
    )

    Environment

    • ty 0.0.89, VS Code ty extension 2026.80.0
    • agent-framework-core 1.21.0, agent-framework-foundry 1.14.1, agent-framework-openai 1.16.0
    • Python 3.14.4

    Caveat: the error appears in the VS Code ty extension (language server). Running ty check from the command line with the same ty version and virtual environment passes for both versions of the snippet, so the CLI may not reproduce it.

    A possible fix in agent-framework: let default_options accept a plain mapping without that type taking part in the type variable that client also uses.

    This comment was written with help from GitHub Copilot.

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

Metadata

Metadata

Labels

agentsUsage: [Issues, PRs], Target: Single agentpythonUsage: [Issues, PRs], Target: Pythonrequested-info

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions