Add framework helpers for LangChain, PydanticAI, and OpenAI Agents SDK#11
Closed
fede-kamel wants to merge 4 commits into
Closed
Add framework helpers for LangChain, PydanticAI, and OpenAI Agents SDK#11fede-kamel wants to merge 4 commits into
fede-kamel wants to merge 4 commits into
Conversation
Bug fixes: - Fix 401 retry silently dropping responses when refresh fails. The generator now ends gracefully and the caller receives the original 401 instead of no response at all. - Fix incorrect return type on OciSessionAuth._load_private_key (str -> Any). The OCI SDK returns a cryptography key object, not a string. Improvements: - Add OciAuthRefreshError exception with cause tracking and elapsed time since last successful refresh, for better diagnostics. - Track refresh failures via _last_refresh_error attribute (cleared on success, set on failure) so callers can inspect auth health. - Reduce logging noise: init and routine refresh checks moved from INFO to DEBUG. Only actual refresh completions stay at INFO. Scheduled refresh failures log at WARNING (not silent exception). 401 retry failures log at ERROR with actionable context. Testing: - Expand unit tests from 9 to 20 covering: signing path with header stripping, POST body signing, 401 retry behavior, non-401 passthrough, refresh failure tracking, error clearing on success, OciAuthRefreshError formatting, session auth missing key_file, config reload on refresh. - Add integration test infrastructure: conftest.py with env-based config loading, session-scoped fixtures, and requires_oci skip marker. - Add tests/.env.example template for integration test configuration. - Add 4 live integration tests: sync/async Responses API, raw httpx signing, and header stripping verification. Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
- Fix SIM105: replace try/except StopIteration/pass with contextlib.suppress(StopIteration) - Fix B011: replace assert False with pytest.raises - Apply isort + ruff format to all changed files - Ensure isort → ruff format → ruff check --fix produces stable output Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
- Remove dependency on model echoing exact text -- assertions now verify non-empty responses and HTTP 200 status instead of specific content, since model output is non-deterministic. - Expand .env.example with prerequisites: how to create a GenAI Project, which models are known to work/not work on /openai/v1, and auth setup instructions. Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
…AI Agents SDK
The OCI Enterprise AI Agents documentation [1] lists LangChain, LangGraph,
PydanticAI, and OpenAI Agents SDK as compatible frameworks. In practice,
none of them work out of the box because:
1. The /openai/v1 endpoint requires an OpenAI-Project header with the
GenAI Project OCID. No framework besides the raw OpenAI SDK exposes
a `project` parameter -- ChatOpenAI, PydanticAI Agent, etc. have
no way to set this header.
2. OCI IAM auth requires a custom httpx.Auth handler. Only frameworks
that accept a custom http_client can use it.
This commit adds builder functions that solve both problems using each
framework's public API -- no monkey-patches, no undocumented internals:
- build_openai_client / build_openai_async_client (OpenAI SDK)
- configure_openai_agents (OpenAI Agents SDK)
- build_langchain_chat (LangChain / LangGraph)
- build_pydantic_ai_model (PydanticAI)
- build_http_client / build_async_http_client (low-level, any framework)
Each builder handles OCI signing + OpenAI-Project header injection,
with region validation to prevent URL injection. Framework packages
are optional dependencies (pip install oci-genai-auth[langchain], etc.).
Includes 15 unit tests and 5 live integration tests against the OCI
endpoint, all passing.
[1] https://docs.oracle.com/en-us/iaas/Content/generative-ai/oci-openai.htm
Signed-off-by: Federico Kamelhar <federico.kamelhar@oracle.com>
Contributor
Author
|
@shenoyvvarun This builds on #10 (auth fixes). The OCI docs list LangChain, PydanticAI, and other frameworks as compatible, but none of them work out of the box because |
Contributor
Author
|
@shenoyvvarun Follow-up on this one as well — depends on #10 for the auth fixes. Ready for review. Thanks! |
4 tasks
Contributor
|
Thank you for making this change, we really appreciate your contribution. I will most likely reject this PR waiting on @ericjy 's feedback.
Also, Sharing the examples for the libraries you mentioned above Langchain (PT mode),Langchain (Non PT mode)Pydantic AI (Non-PT mode) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The OCI Enterprise AI Agents documentation lists these frameworks as compatible:
None of them work out of the box. Both OCI GenAI endpoints require custom headers that no framework exposes as parameters:
/openai/v1OpenAI-Project(GenAI Project OCID)/openai/v1opc-compartment-id/20231130/actions/v1opc-compartment-id/20231130/actions/v1opc-compartment-idVerified against the live endpoint — every combination without the required header returns 400. There is no combination that works with just
base_url+api_keylike other providers (Together AI, Groq, Fireworks, Azure).ChatOpenAI,PydanticAI Agent, etc. have noprojectorcompartment_idparameter — there is no way to set these headers through their public API without workarounds.Additionally, OCI IAM auth requires a custom
httpx.Authhandler to sign requests. Only frameworks that accept a customhttp_client=parameter can use it.This means a developer following Oracle's docs will write:
Solution
One-liner builders that make the documented claim true:
Each builder handles OCI signing + required header injection using only the framework's public API — no monkey-patches, no undocumented internals:
build_langchain_chathttpx.Clientviahttp_client=build_pydantic_ai_modelAsyncOpenAIviaOpenAIProvider(openai_client=)configure_openai_agentsset_default_openai_client()with pre-signedAsyncOpenAIbuild_openai_clientcommon.pypatternbuild_http_clientOptional dependencies keep the library lightweight:
Includes region validation (regex) to prevent URL injection via the
regionparameter.Test plan
OCI_GENAI_*env varsFuture work
CrewAI, AutoGen, and LlamaIndex require workarounds using undocumented framework internals (litellm client passthrough, deepcopy patching, model name validator monkey-patching). These are intentionally excluded from this PR — they work today but could break on framework updates. They can be contributed as examples or added once those frameworks support custom
http_clientparameters.Fixes #13