Skip to main content

LangExtract provider plugin for OpenRouter API

Project description

LangExtract OpenRouter Provider

A LangExtract provider plugin for OpenRouter.

Installation

# Using uv (recommended)
uv pip install langextract-provider-openrouter

# Or using pip
pip install langextract-provider-openrouter

Quick Start

  1. Set your OpenRouter API key:

    export OPENROUTER_API_KEY=your_key_here
    
  2. Use it in LangExtract:

    import langextract as lx
    from langextract import data
    
    # LangExtract requires at least one example
    examples = [
        data.ExampleData(
            text="John Doe works at Google",
            extractions=[
                data.Extraction(extraction_class="Person", extraction_text="John Doe"),
                data.Extraction(extraction_class="Company", extraction_text="Google")
            ]
        )
    ]
    
    # Use the 'openrouter/' prefix followed by the OpenRouter model ID
    result = lx.extract(
        text_or_documents="Alice Smith joined Apple",
        prompt_description="Extract people and companies",
        examples=examples,
        model_id="openrouter/google/gemini-2.0-flash-exp:free"
    )
    
    for ext in result.extractions:
        print(f"{ext.extraction_class}: {ext.extraction_text}")
    

Supported Models

This provider supports any model available on OpenRouter by prefixing the model ID with openrouter/. Common examples include:

  • openrouter/google/gemini-3-flash-preview
  • openrouter/google/gemini-3-pro-preview
  • openrouter/google/gemini-2.5-flash-preview-09-2025
  • openrouter/Qwen/Qwen2.5-VL-72B-Instruct
  • openrouter/openai/gpt-5

Configuration Options

Customize your requests using the provider_options dictionary.

Reasoning Effort

Control the reasoning effort for models that support it.

# Create a model with high reasoning effort
config = factory.ModelConfig(
    model_id="openrouter/Qwen/Qwen2.5-VL-72B-Instruct",
    provider_kwargs={
        "provider_options": {
            "effort": "high"
        }
    }
)

Routing/Provider Options

These options control how OpenRouter acts as a router:

  • sort: Strategy for provider selection ("price", "latency", "throughput"). Default: "price".
  • ignore: List of provider names to exclude (e.g., ["Parasail"]).
  • allow_fallbacks: Boolean to allow falling back to other providers. Default: True.
  • data_collection: "allow" or "deny". Default: "allow".

Usage Example (Inference Override)

result = lx.extract(
    # ... inputs ...
    # Example: GPT-5
    model_id="openrouter/openai/gpt-5",
    provider_options={
        "effort": "medium",
        "sort": "throughput",
        "ignore": ["SlowProvider"]
    }
)

Development

cd langextract-openrouter
uv venv --python 3.12
# Install dependencies
uv pip install -e ../langextract
uv pip install -e .
uv pip install pytest requests
# Run tests
uv run pytest

Complete Example

For a complete, runnable example, see example_usage.py in the root directory.

import langextract as lx
from langextract import data

examples = [
    data.ExampleData(
        text="John Doe works at Google",
        extractions=[
            data.Extraction(extraction_class="Person", extraction_text="John Doe"),
            data.Extraction(extraction_class="Company", extraction_text="Google")
        ]
    )
]

result = lx.extract(
    "Alice Smith joined Apple", 
    "Extract people and companies", 
    examples=examples,
    model_id="openrouter/google/gemini-2.0-flash-exp:free"
)
for ext in result.extractions:
    print(f"{ext.extraction_class}: {ext.extraction_text}")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langextract_provider_openrouter-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file langextract_provider_openrouter-0.1.1.tar.gz.

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.1.tar.gz
Algorithm Hash digest
SHA256 fb031669227aedccb589bc524e910267d52417585d3d0e9345e1abf464c0ac19
MD5 7e5d3e010b1ae01057b8e0574a5d0d9b
BLAKE2b-256 0bd3d8713e6a692a18a366aaafd5390b3de143b6d022c1fdf4e3e39dcf76d676

See more details on using hashes here.

File details

Details for the file langextract_provider_openrouter-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for langextract_provider_openrouter-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 046d4f04db190b07a5ee8c5177e488a9c48e60dd4fef426008df3991ffd5fb4f
MD5 d9a820d12ce1872c25ce2972628c9f5d
BLAKE2b-256 a37a4a8238fd42a276c8d48441125250e782aab150353cefbfdba92479bf3393

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page