google / google/langextract

LangExtract Integration Challenges (Gemini SSL & Ollama API Key)

Open
#145 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Python
Stars
38.6k
Forks
2.7k
Avg merge
3d 15h
Merged PRs (30d)
3

Description

## Issue Summary
This issue details persistent challenges encountered during the integration of the `langextract` library, specifically concerning SSL certificate verification with Google Gemini models and an unexpected API key requirement when attempting to use local Ollama models. These issues are currently blocking successful structured data extraction.

## Environment
- **Operating System**: Windows 11
- **Python Version**: (Assumed from `C:\dev\python.exe` path, likely Python 3.x)
- **LangExtract Version**: Latest (as of attempts)
- **LLM Providers**: Google Gemini (cloud), Ollama (local, gemma2:2b)

## Problem Description

### 1. Persistent SSL Certificate Verification Failure with Google Gemini
When attempting to use `langextract` with Google Gemini models, the extraction consistently fails with an `[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1018)` error. This prevents any successful communication with the Gemini API.

**Observed Error Output (from `test_production_extraction.py`):**
```
Gemini API error: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1018)
```

**Attempted Solutions:**
- Setting global environment variables:
- `PYTHONHTTPSVERIFY = '0'`
- `CURL_CA_BUNDLE = ''`
- `REQUESTS_CA_BUNDLE = ''`
- Disabling `urllib3` warnings: `urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)`
- Direct monkey-patching of `ssl._create_unverified_context` and `urllib3.util.connection.create_connection` (later reverted due to ineffectiveness and complexity).
- Ensuring `certifi` is up to date.

**Conclusion on SSL Issue**: The issue appears to be a deep-seated system-level SSL certificate problem on the Windows environment, which `langextract`'s underlying HTTP client (likely `requests` or `urllib3`) cannot bypass, even with aggressive in-code and environment-variable-based workarounds.

### 2. Unexpected API Key Requirement for Local Ollama Models
Following the persistent Gemini SSL issue, an attempt was made to switch to local LLMs via Ollama, as `langextract` documentation states "On-device models don't require an API key." However, `langextract.lx.extract` still consistently demands an API key, even when configured for Ollama.

**Observed Error Output (from `debug_ollama_langextract.py` and `test_production_extraction.py`):**
```
[DEBUG] USE_OLLAMA_FLAG read as: True
[DEBUG] Inside test_production_extractor, use_ollama is: True
Using Ollama with model: gemma2:2b at http://localhost:11434
[ERROR] Ollama extraction test failed: API key must be provided for cloud-hosted models via the api_key parameter or the LANGEXTRACT_API_KEY environment variable
ValueError: API key must be provided for cloud-hosted models via the api_key parameter or the LANGEXTRACT_API_KEY environment variable
```
**Ollama Server Status**: Confirmed Ollama server is running and accessible locally via `curl http://localhost:11434`.

**Attempted Solutions:**
- Conditional logic to set `self.gemini_api_key = None` when `use_ollama` is true.
- Conditional logic to omit the `api_key` parameter from `lx.extract` when `self.use_ollama` is true.
- Aggressive environment variable manipulation (deleting `LANGEXTRACT_API_KEY`, `GOOGLE_API_KEY`, `GEMINI_API_KEY` from `os.environ`) at the very top of the Python scripts, *before* `langextract` is imported, to ensure a clean environment.

**Conclusion on Ollama Issue**: Despite all attempts to explicitly configure `langextract` for local Ollama usage without an API key, the library's internal validation still triggers the `ValueError`. This suggests that `langextract` might have an unconditional API key check that cannot be bypassed through standard configuration or environment variable manipulation for local models, contrary to its documentation.

## Steps to Reproduce
1. Ensure Ollama is installed and `gemma2:2b` model is pulled (`ollama pull gemma2:2b`).
2. Ensure Ollama server is running (`ollama serve`).
3. Set `USE_OLLAMA=true` in your `.env` file.
4. Run `python debug_ollama_langextract.py` from the project root.
5. Observe the `ValueError: API key must be provided...` despite Ollama configuration.

## Expected Behavior
When `langextract` is configured to use a local Ollama model via `model_url` and `model_id`, it should not require or validate any cloud API keys (e.g., `api_key` parameter, `LANGEXTRACT_API_KEY` environment variable).

## Suggested Resolution (for LangExtract developers)
- Investigate `langextract`'s internal API key validation logic to ensure it correctly bypasses API key requirements when `model_url` is provided for local models (e.g., Ollama).
- Clarify documentation regarding API key requirements for local models if there are specific environment variable interactions or configurations needed beyond simply providing `model_url`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.