Skip to main content

OpenAiChatModel

Struct OpenAiChatModel 

Source
pub struct OpenAiChatModel { /* private fields */ }
Available on crate feature tool-loop only.
Expand description

A ToolModel that sends each turn as POST {base}/chat/completions.

use a2a_protocol_server::tool_loop::OpenAiChatModel;

// llama.cpp's llama-server, with Qwen3's thinking turned off.
let mut extra = serde_json::Map::new();
extra.insert("chat_template_kwargs".into(), serde_json::json!({"enable_thinking": false}));
let model = OpenAiChatModel::new("http://127.0.0.1:8080/v1", "qwen3:1.7b")?
    .with_extra_body(extra);

Tool-call arguments are read whether the server sends them as a JSON string, as OpenAI does, or as an object, as some compatible servers do.

Implementations§

Source§

impl OpenAiChatModel

Source

pub fn new( base_url: &str, model: impl Into<String>, ) -> Result<OpenAiChatModel, ModelError>

A client for model at base_url, the API’s base including its version segment (https://api.openai.com/v1, http://127.0.0.1:8080/v1).

§Errors

ModelError::Config when base_url is not an http:// or https:// URL, or is https:// in a build without tls-rustls — refused here rather than at the first turn.

Source

pub fn with_tls_config(self, tls_config: ClientConfig) -> OpenAiChatModel

Available on crate feature tls-rustls only.

Verifies the server against tls_config instead of Mozilla’s roots: a private CA, or a client certificate for mutual TLS.

Source

pub fn with_api_key(self, key: impl Into<String>) -> OpenAiChatModel

Sends key as Authorization: Bearer {key}. Local servers need none.

Source

pub const fn with_timeout(self, timeout: Duration) -> OpenAiChatModel

Bounds each turn — connecting, sending and reading the whole response. Default DEFAULT_TIMEOUT.

Source

pub const fn with_max_response_bytes(self, max: usize) -> OpenAiChatModel

Caps the response body read, in bytes. Default DEFAULT_MAX_RESPONSE_BYTES.

Source

pub const fn with_stream_connection_reuse(self) -> OpenAiChatModel

Lets a streamed turn’s connection be reused for the next turn.

By default each streamed turn asks for its connection to be closed after it (Connection: close): llama.cpp’s llama-server says keep-alive and then closes the connection just after a streamed answer (seen on llama.cpp 9daa0aa, 2026-11-13: three of three attempts to reuse the connection found it closed), and a pooled connection would carry the next turn into that close (“connection closed before message completed”). An endpoint that keeps its connections (a hosted provider, a proxy) gains a handshake per turn from this, which over TLS and a remote network is not free: call this for such an endpoint (adversarial review N16, 2026-11-13). The cost was not measured here: the only endpoint at hand was a local one.

Source

pub fn with_extra_body(self, fields: Map<String, Value>) -> OpenAiChatModel

Adds fields to every request body: temperature, chat_template_kwargs, tool_choice and the like. The fields this client writes itself — model, messages, and tools when there are tools — win over an extra of the same name.

Trait Implementations§

Source§

impl Debug for OpenAiChatModel

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl ToolModel for OpenAiChatModel

Source§

fn provider_name(&self) -> &'static str

openai: the conventions name the API a client speaks, not the server behind it (“multiple systems … are accessible by OpenAI client libraries. In such cases, gen_ai.provider.name is set to openai”).

Source§

fn server(&self) -> Option<(&str, u16)>

The endpoint’s host, without an IPv6 address’s brackets, and its port, or the scheme’s when it names none.

Source§

fn complete_streaming<'a>( &'a self, request: ChatRequest<'a>, text: &'a TextSink, ) -> Pin<Box<dyn Future<Output = Result<ModelTurn, ModelError>> + Send + 'a>>

Asks for the turn as a stream ("stream": true, with its usage), and hands text each piece of the answer as it arrives. Each streamed turn has a connection of its own (Connection: close) unless with_stream_connection_reuse was called.

Source§

fn model_name(&self) -> &str

The model asked for, recorded as gen_ai.request.model and in the name of each turn’s span (chat {model}).
Source§

fn complete<'a>( &'a self, request: ChatRequest<'a>, ) -> Pin<Box<dyn Future<Output = Result<ModelTurn, ModelError>> + Send + 'a>>

Runs one turn. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> IntoRequest<T> for T

§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
§

impl<L> LayerExt<L> for L

§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in [Layered].
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more