Home / Function/ test_callable_api_key_async() — langchain Function Reference

test_callable_api_key_async() — langchain Function Reference

Architecture documentation for the test_callable_api_key_async() function in test_base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  90150c47_173f_4845_54c9_02faec3e1149["test_callable_api_key_async()"]
  bd382a4e_442c_13ae_530c_6e34bc43623d["test_base.py"]
  90150c47_173f_4845_54c9_02faec3e1149 -->|defined in| bd382a4e_442c_13ae_530c_6e34bc43623d
  style 90150c47_173f_4845_54c9_02faec3e1149 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/tests/integration_tests/chat_models/test_base.py lines 81–112

async def test_callable_api_key_async(monkeypatch: pytest.MonkeyPatch) -> None:
    original_key = os.environ["OPENAI_API_KEY"]

    calls = {"sync": 0, "async": 0}

    def get_openai_api_key() -> str:
        calls["sync"] += 1
        return original_key

    async def get_openai_api_key_async() -> str:
        calls["async"] += 1
        return original_key

    monkeypatch.delenv("OPENAI_API_KEY")

    model = ChatOpenAI(model="gpt-4.1-mini", api_key=get_openai_api_key)
    response = model.invoke("hello")
    assert isinstance(response, AIMessage)
    assert calls["sync"] == 1

    response = await model.ainvoke("hello")
    assert isinstance(response, AIMessage)
    assert calls["sync"] == 2

    model = ChatOpenAI(model="gpt-4.1-mini", api_key=get_openai_api_key_async)
    async_response = await model.ainvoke("hello")
    assert isinstance(async_response, AIMessage)
    assert calls["async"] == 1

    with pytest.raises(ValueError):
        # We do not create a sync callable from an async one
        _ = model.invoke("hello")

Domain

Subdomains

Frequently Asked Questions

What does test_callable_api_key_async() do?
test_callable_api_key_async() is a function in the langchain codebase, defined in libs/partners/openai/tests/integration_tests/chat_models/test_base.py.
Where is test_callable_api_key_async() defined?
test_callable_api_key_async() is defined in libs/partners/openai/tests/integration_tests/chat_models/test_base.py at line 81.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free