openai_compat_backend
openai_compat_backend
¶
Generic OpenAI-compatible HTTP execution for profile-driven instruments.
The backend speaks the standard OpenAI chat/completions protocol and works against any compatible HTTP service, including hosted and local servers.
Usage is internal only — it is constructed by
marianne.daemon.baton.backend_pool._build_openai_family_backend
when dispatching an HttpProfile whose schema_family is openai.
Key design decisions: - Extends Backend ABC with HttpxClientMixin for lazy httpx client lifecycle. - Model is specified per-request, allowing per-sheet instrument overrides. - Rate limit detection from HTTP 429 status and Retry-After header. - Token usage extracted from the standard OpenAI usage response field. - Free-tier model support (no cost for many models).
Security: API keys are NEVER logged. The key is read from environment and passed only in the Authorization header. The logging infrastructure uses SENSITIVE_PATTERNS to automatically redact fields containing 'api_key', 'token', 'secret', etc.
Classes¶
OpenAICompatibleBackend
¶
OpenAICompatibleBackend(model, base_url, *, api_key_env=None, max_tokens=16384, temperature=0.7, timeout_seconds=300.0, endpoint='/chat/completions', response_format=None, loaded_profile=None)
Bases: HttpxClientMixin, Backend
Run prompts through a profile-selected OpenAI-compatible API.
Connection details come from an instrument profile. The executor carries no provider-specific endpoint, model, authentication, or headers.
Example usage::
backend = OpenAICompatibleBackend(
model="minimax/minimax-m1-80k",
base_url="https://openrouter.ai/api/v1",
api_key_env="OPENROUTER_API_KEY",
)
result = await backend.execute("Explain quicksort")
Initialize a profile-selected OpenAI-compatible transport.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Model ID understood by the selected endpoint. |
required |
base_url
|
str
|
API base URL (without endpoint path). |
required |
api_key_env
|
str | None
|
Optional environment variable containing an API key. |
None
|
max_tokens
|
int
|
Maximum tokens for response. |
16384
|
temperature
|
float
|
Sampling temperature (0.0-2.0). |
0.7
|
timeout_seconds
|
float
|
Maximum time for API request. |
300.0
|
endpoint
|
str
|
Profile-selected OpenAI chat-completions endpoint. |
'/chat/completions'
|
response_format
|
dict[str, Any] | None
|
Opt-in structured-output object from the instrument profile (None = omit the key; the legacy payload). Validated here; per-sheet overrides come through set_response_format(). |
None
|
Source code in src/marianne/execution/instruments/openai_compat_backend.py
Attributes¶
Methods:¶
effort_refusal
¶
Why this instrument cannot run a sheet at level (None = it can).
Judged against the loaded profile's effort: block (delivery
body_field) for model, or this backend's model when None.
A backend built without a profile carries no mapping.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
set_api_key
¶
Set a key supplied by the runtime keyring, if one is available.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
apply_overrides
¶
Apply per-sheet overrides for the next execution.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
clear_overrides
¶
Restore original backend parameters after per-sheet execution.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
set_preamble
¶
Set the dynamic preamble for the next execution.
Direct-use contract only: the pooled dispatch path carries the
per-attempt preamble request-locally (SheetRequestState) — see
Backend.set_preamble. On this shared HTTP singleton a mutable
preamble is the W-F3 sequential-smear seam; clear_overrides()
resets it.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
set_response_format
¶
Set an opt-in structured-output response_format for this sheet.
Direct-use contract only (exclusively-owned backends, tests, and
the adapter's capability probe): the pooled dispatch path resolves
the sheet's value once and carries it request-locally on
SheetRequestState — never through this mutable slot, which is
the W-F1/W-F2 transfer seam on the shared HTTP singleton. The
value is validated by the shared wire-contract validator — invalid
shapes raise ValueError. clear_overrides() restores the
profile default at release.
Source code in src/marianne/execution/instruments/openai_compat_backend.py
set_prompt_extensions
¶
Set prompt extensions for the next execution.
set_output_log_path
¶
Set base path for real-time output logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | None
|
Base path for log files (without extension), or None to disable. |
required |
Source code in src/marianne/execution/instruments/openai_compat_backend.py
execute
async
¶
Execute a prompt via the selected OpenAI-compatible API.
Sends a chat completion request and returns the generic response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send. |
required |
timeout_seconds
|
float | None
|
Per-call HTTP timeout override for direct calls. A resolved sheet request uses its own HTTP timeout, falling back to the profile default when unset. Connect timeout is capped at ten seconds within that per-request timeout. |
None
|
request
|
SheetRequestState | None
|
Per-sheet state resolved once at dispatch. When
provided, EVERY payload field it carries is request-local
(W-F1/W-F2/W-F3): |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
ExecutionResult with API response and metadata. |
Source code in src/marianne/execution/instruments/openai_compat_backend.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
health_check
async
¶
Check if the selected API is reachable and authenticated.
Uses the /models endpoint (lightweight, no token consumption) to verify connectivity and authentication.
Returns:
| Type | Description |
|---|---|
bool
|
True if healthy, False otherwise. |
Source code in src/marianne/execution/instruments/openai_compat_backend.py
availability_check
async
¶
Check if the backend can be initialized without consuming API quota.
Verifies that the API key is present and the httpx client can be
created. Does NOT make any HTTP requests.
Client initialization exceptions log
openai_compatible_initialization_failed at WARNING and return False.
A missing required API key returns False before attempting initialization.