Skip to content

openai-like provider does not respect NPCSH_API_URL environment variable #130

@celeria-ai

Description

@celeria-ai

Issue Summary

When using the openai-like provider, the NPCSH_API_URL environment variable is not respected for setting the API base URL. Users are forced to specify api_url in each NPC configuration instead of being able to set it globally via environment variable.

Problem Location

File: npcsh/_state.py
Function: setup_shell()
Lines: ~4145-4155

Current Behavior

During team initialization in setup_shell(), NPCs are assigned default model and provider values from the environment:

for npc_name, npc_obj in team.npcs.items():
    if not npc_obj.model:
        npc_obj.model = initial_state.chat_model
    if not npc_obj.provider:
        npc_obj.provider = initial_state.chat_provider

However, there's no corresponding check to inject NPCSH_API_URL for NPCs using the openai-like provider that don't have an explicit api_url set.

Expected Behavior

Similar to how default model and provider are injected during initialization, NPCs using openai-like provider without an explicit api_url should receive the value from NPCSH_API_URL environment variable.

Use Case

Users trying to use Ollama Cloud services (or other OpenAI-compatible APIs) with the openai-like provider cannot set the API URL globally via NPCSH_API_URL. They are forced to specify api_url in each NPC configuration, which is inconvenient and inconsistent with how other configuration values are handled.

Proposed Fix

In npcsh/_state.py, during the setup_shell() function where default model and provider are set, add logic to inject NPCSH_API_URL for openai-like provider NPCs:

for npc_name, npc_obj in team.npcs.items():
    if not npc_obj.model:
        npc_obj.model = initial_state.chat_model
    if not npc_obj.provider:
        npc_obj.provider = initial_state.chat_provider
    # Inject NPCSH_API_URL for openai-like provider if not explicitly set
    if not getattr(npc_obj, 'api_url', None) and npc_obj.provider == 'openai-like':
        npc_obj.api_url = os.environ.get("NPCSH_API_URL")

Apply the same logic to forenpc if present.

Why npcsh Layer

The fix belongs in npcsh during team initialization, where npcsh-specific environment variables should be injected into NPC configurations. This keeps the core npcpy library agnostic while allowing npcsh to provide configuration defaults at the appropriate layer.

Testing

Test scenarios:

  1. Set NPCSH_API_URL=https://ollama-cloud.example.com/v1 and use provider: openai-like without explicit api_url - should use the env var
  2. Explicit api_url parameter in NPC config should still take precedence over environment variable
  3. Verify existing behavior for other providers is not affected

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions