Skip to content

Commit 59be368

Browse files
authored
feat(messages): retrieve provider API key for Messages API passthrough requests (#5981)
Replace hardcoded "no-key-required" header value in _passthrough_request and _passthrough_count_tokens with dynamically retrieved API keys from the provider configuration. This allows authenticated providers like vLLM to successfully authenticate passthrough requests to their native /v1/messages endpoints. The implementation preferentially calls _get_api_key_from_config_or_provider_data() to support both static configuration and per-request credential overrides via request headers, falling back to get_api_key() if unavailable. Note: This introduces a temporary abstraction violation that will be refactored in a future change to use a proper provider interface for Messages API support. Fixes providers requiring authentication (e.g., vLLM) when using native passthrough mode.
1 parent 7b2d07c commit 59be368

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

  • src/ogx/providers/inline/messages

src/ogx/providers/inline/messages/impl.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,11 +472,21 @@ async def _passthrough_request(
472472
# Use the provider_resource_id (model name without provider prefix)
473473
provider_model = request.model
474474
router = self.inference_api
475+
api_key = "no-key-required"
475476
if hasattr(router, "routing_table"):
476477
try:
477478
obj = await router.routing_table.get_object_by_identifier("model", request.model)
478479
if obj:
479480
provider_model = obj.provider_resource_id
481+
provider_impl = await router.routing_table.get_provider_impl(obj.identifier)
482+
# TODO: this is a sever abstration violation. this all needs to be refactored
483+
# to use a proper provider interface for messages
484+
if hasattr(provider_impl, "_get_api_key_from_config_or_provider_data"):
485+
key = provider_impl._get_api_key_from_config_or_provider_data()
486+
else:
487+
key = provider_impl.get_api_key() if hasattr(provider_impl, "get_api_key") else None
488+
if key:
489+
api_key = key
480490
except (KeyError, ValueError, AttributeError):
481491
logger.debug("Failed to resolve provider model name, using original", model=request.model)
482492

@@ -485,7 +495,7 @@ async def _passthrough_request(
485495
headers = {
486496
"content-type": "application/json",
487497
"anthropic-version": ANTHROPIC_VERSION,
488-
"x-api-key": "no-key-required",
498+
"x-api-key": api_key,
489499
}
490500

491501
if request.stream:
@@ -571,11 +581,21 @@ async def _passthrough_count_tokens(
571581
# Use the provider_resource_id (model name without provider prefix)
572582
provider_model = request.model
573583
router = self.inference_api
584+
api_key = "no-key-required"
574585
if hasattr(router, "routing_table"):
575586
try:
576587
obj = await router.routing_table.get_object_by_identifier("model", request.model)
577588
if obj:
578589
provider_model = obj.provider_resource_id
590+
provider_impl = await router.routing_table.get_provider_impl(obj.identifier)
591+
# TODO: this needs to be rafactored to avoid abstraction violations and remove
592+
# duplicated logic with _passthrough_request
593+
if hasattr(provider_impl, "_get_api_key_from_config_or_provider_data"):
594+
key = provider_impl._get_api_key_from_config_or_provider_data()
595+
else:
596+
key = provider_impl.get_api_key() if hasattr(provider_impl, "get_api_key") else None
597+
if key:
598+
api_key = key
579599
except (KeyError, ValueError, AttributeError):
580600
logger.debug("Failed to resolve provider model name, using original", model=request.model)
581601

@@ -584,7 +604,7 @@ async def _passthrough_count_tokens(
584604
headers = {
585605
"content-type": "application/json",
586606
"anthropic-version": ANTHROPIC_VERSION,
587-
"x-api-key": "no-key-required",
607+
"x-api-key": api_key,
588608
}
589609

590610
resp = await self._client.post(url, json=body, headers=headers, timeout=30)

0 commit comments

Comments
 (0)