Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix(langchain): send configured authentication headers to LangServe (#202)

Rudy Mizrahi Celekli0139f60e1ff8
diff --git a/ifixai/providers/langchain.py b/ifixai/providers/langchain.py
index 15257b0..f4789d5 100644
--- a/ifixai/providers/langchain.py
+++ b/ifixai/providers/langchain.py
@@ -15,6 +15,7 @@ from ifixai.providers.base import (
ProviderResponseError,
ProviderTimeoutError,
)
+from ifixai.providers.http import _build_auth_headers
DEFAULT_ENDPOINT = "http://localhost:8000"
@@ -44,7 +45,7 @@ class LangChainProvider(ChatProvider):
try:
async with aiohttp.ClientSession(timeout=timeout) as session:
- async with session.post(url, json=payload) as response:
+ async with session.post(url, json=payload, headers=_build_auth_headers(config)) as response:
if response.status == 401 or response.status == 403:
raise ProviderAuthError(
provider="langchain",
diff --git a/ifixai/tests/test_langchain_transport_native.py b/ifixai/tests/test_langchain_transport_native.py
index 0d02255..ac90268 100644
--- a/ifixai/tests/test_langchain_transport_native.py
+++ b/ifixai/tests/test_langchain_transport_native.py
@@ -103,3 +103,35 @@ async def test_disconnected_endpoint_remains_connection_error():
await LangChainProvider().send_message(
[ChatMessage(role="user", content="owned prompt")], config(url)
)
+
+
[email protected]("auth,key,custom,required", [
+ ("bearer", "owned-key", {}, {"Authorization": "Bearer owned-key"}),
+ ("basic", "owned:password", {}, {"Authorization": "Basic b3duZWQ6cGFzc3dvcmQ="}),
+ ("api_key", "owned-key", {}, {"X-API-Key": "owned-key"}),
+ ("none", "", {"X-Owned-Token": "owned-token"}, {"X-Owned-Token": "owned-token"}),
+])
+async def test_langserve_authenticated_invoke_uses_configured_headers(monkeypatch, auth, key, custom, required):
+ monkeypatch.delenv("IFIXAI_EXTRA_HEADERS", raising=False)
+ seen = []
+ async def invoke(request):
+ seen.append(dict(request.headers))
+ if any(request.headers.get(name) != value for name, value in required.items()):
+ return web.json_response({"detail": "owned authentication required"}, status=401)
+ return web.json_response({"output": "owned authenticated reply"})
+
+ app = web.Application()
+ app.router.add_post("/invoke", invoke)
+ runner = web.AppRunner(app)
+ await runner.setup()
+ site = web.TCPSite(runner, "127.0.0.1", 0)
+ await site.start()
+ url = f"http://127.0.0.1:{site._server.sockets[0].getsockname()[1]}"
+ try:
+ config = ProviderConfig(provider="langchain", endpoint=url, auth_method=auth,
+ api_key=key, extra_headers=custom, max_retries=0)
+ assert await LangChainProvider().send_message([ChatMessage(content="hello")], config) == "owned authenticated reply"
+ assert len(seen) == 1
+ finally:
+ await runner.cleanup()