Skip to content

Support OPENAI_BASE_URL routing and JSON parsing#453

Open
bpulluta wants to merge 1 commit into
mainfrom
litellm-dev
Open

Support OPENAI_BASE_URL routing and JSON parsing#453
bpulluta wants to merge 1 commit into
mainfrom
litellm-dev

Conversation

@bpulluta
Copy link
Copy Markdown
Collaborator

Summary

  • add OPENAI_BASE_URL and OPENAI_API_KEY env mapping for openai client config
  • keep explicit client kwargs precedence for backward compatibility
  • add fallback parser for responses that include prose before JSON
  • add unit tests for openai config env handling and parsing fallback

Validation

  • pixi run -e pdev ruff check compass/llm/config.py compass/utilities/parsing.py tests/python/unit/utilities/test_utilities_parsing.py tests/python/unit/llm/test_config.py
  • pixi run -e pdev pytest -q tests/python/unit/utilities/test_utilities_parsing.py tests/python/unit/llm/test_config.py

Copilot AI review requested due to automatic review settings May 28, 2026 21:42
@bpulluta bpulluta changed the title Support OPENAI_BASE_URL routing and robust JSON parsing Support OPENAI_BASE_URL routing and JSON parsing May 28, 2026
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 56.63%. Comparing base (9c17ada) to head (3b12270).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
compass/llm/config.py 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #453      +/-   ##
==========================================
+ Coverage   56.11%   56.63%   +0.51%     
==========================================
  Files          63       66       +3     
  Lines        6080     6298     +218     
  Branches      591      620      +29     
==========================================
+ Hits         3412     3567     +155     
- Misses       2598     2656      +58     
- Partials       70       75       +5     
Flag Coverage Δ
unittests 56.63% <94.73%> (+0.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds OpenAI environment-based client configuration and makes LLM JSON response parsing more tolerant of prose-wrapped JSON, supporting routed OpenAI-compatible endpoints.

Changes:

  • Maps OPENAI_API_KEY and OPENAI_BASE_URL into OpenAI client kwargs while preserving explicit kwargs precedence.
  • Adds fallback parsing for embedded JSON payloads after strict JSON decoding fails.
  • Adds unit tests for OpenAI config env handling and prose-before-JSON parsing.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
compass/llm/config.py Adds OpenAI env var mapping for client initialization.
compass/utilities/parsing.py Adds fallback recovery for embedded JSON objects or arrays.
tests/python/unit/llm/test_config.py Adds tests for OpenAI and Azure client kwargs env handling.
tests/python/unit/utilities/test_utilities_parsing.py Adds coverage for prose-prefixed JSON parsing.

Comment on lines +99 to +107
for start_char in ("{", "["):
start_ind = content.find(start_char)
while start_ind != -1:
try:
parsed_content, __ = decoder.raw_decode(content[start_ind:])
except json.decoder.JSONDecodeError:
start_ind = content.find(start_char, start_ind + 1)
else:
return parsed_content
Comment on lines +77 to +97
def _parse_first_json_payload(content):
"""[NOT PUBLIC API] Parse first valid JSON payload embedded in text

Parameters
----------
content : str
Text that may contain one or more JSON payloads mixed with
additional non-JSON prose.

Returns
-------
object or None
Parsed JSON payload from the first decodable object/array in
the string. Returns ``None`` if no decodable payload exists.

Notes
-----
This helper scans for ``"{"`` and ``"["`` markers and attempts
``json.JSONDecoder().raw_decode`` from each candidate position
until successful.
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants