LLMClient for IBM watsonx.ai chat (POST /ml/v1/text/chat and /ml/v1/text/chat_stream).
'''Beta - never run against the live service.''' There is no watsonx account behind this project, so nothing here has been checked against the real service (see https://github.com/llm4s/llm4s/issues/1314). The request and response shapes are those IBM's public pages report, cross-checked against IBM's own open-source client code (see ''Evidence'' below). The module's API is not frozen. It replaces the text-generation endpoints (/ml/v1/text/generation and /generation_stream), which IBM's February 2026 release notes deprecate without giving a removal date.
== Evidence ==
A detail is ''SDK-evidenced'' when IBM's own code shows it. That proves IBM's client sends it, which is strong evidence the service accepts it, but it is not a live check. The sources, pinned:
NODE: https://github.com/IBM/watsonx-ai-node-sdk at 47a4a0c (2026-08-05), IBM's Node.js SDK;LCIBM: https://github.com/langchain-ai/langchain-ibm at 6c32b1d (2026-10-05), IBM's LangChain integration,libs/ibm/langchain_ibm/chat_models.py;PY: IBM's Python SDK,ibm-watsonx-ai1.8.0 on PyPI;JAVA: https://github.com/IBM/watsonx-ai-java-sdk at 02246d5 (2026-10-22), IBM's Java SDK, whose test resources include replies recorded from the service (modules/watsonx-ai/src/test/...);DOCS: IBM's documentation page "Building agent-driven workflows with the chat API" (https://www.ibm.com/docs/SSYOK8/wsj/analyze-data/fm-api-chat-tools.html, read 2026-10-30), whose curl examples post to/ml/v1/text/chat?version=...withtool_choice_option: "auto", and whose quoted reply hasfinish_reason: "tool_calls"and a call{"id","type":"function","function":{"name","arguments"}}withargumentsa JSON string.WatsonXDocumentedShapesSpecpins those shapes.
The @Cloud suite WatsonXAssumptionProbeSpec (modules/it) re-checks each of these against a real account and prints which held.
== Authentication ==
The IBM Cloud API key is exchanged at the IAM endpoint for a bearer token that lives an hour. The token is cached and refreshed lazily, five minutes before it expires.
== Request format ==
The conversation is sent as structured messages with roles (system, user, assistant, tool), so content is data and cannot forge a turn: there is no prompt string with role markers, and no stop sequences standing in for them. The model goes in model_id, the project or space in project_id or space_id, and the API version in the version query parameter (WatsonXConfig.apiVersion, by default the date PY sends: org.llm4s.llmconnect.config.WatsonXConfig.DEFAULT_API_VERSION). temperature is always sent, max_tokens when set (SDK-evidenced: NODE types/vml_v1.ts:790-797, a deprecated alias of max_completion_tokens), and top_p when not 1.0.
== Tools ==
CompletionOptions.tools are sent as tools (type: function, function: {name, description, parameters}; the OpenAI strict flag is dropped) with tool_choice_option: "auto". Tool calls in a reply (message.tool_calls, whole, or streamed as delta.tool_calls pieces merged by index) become ToolCalls. An assistant turn's calls go back as tool_calls, and a ToolMessage as a tool message with its tool_call_id. arguments is a JSON string on the wire (SDK-evidenced: NODE types/messages.ts:19-24) and a JSON object in a ToolCall. A call without an id gets a generated one, defensively: the SDK's type makes id required (NODE types/messages.ts:29-30), and a stream sends it on a call's first piece only (LCIBM chat_models.py:324). Whether the model calls tools, or supports them at all, depends on the model.
== Penalties and response format ==
presencePenalty and frequencyPenalty are sent as presence_penalty and frequency_penalty when not 0.0, their default, so a request that leaves them alone is unchanged (SDK-evidenced: NODE vml_v1.ts:2808,2815 for textChat and 3032,3039 for textChatStream; PY foundation_models/schema/_api.py:270,273; LCIBM chat_models.py:939,969,1664,1670). Their range is the service's to check.
responseFormat is sent as response_format (WatsonXClient.encodeResponseFormat): ResponseFormat.Json as {"type":"json_object"} (NODE types/vml_v1.ts:3283-3310; PY schema/_api.py:225; LCIBM chat_models.py:2147-2149), and ResponseFormat.JsonSchema as {"type":"json_schema","json_schema":{"name","schema","strict"}} (PY schema/_api.py:226,231-234,255-257; LCIBM chat_models.py:2160-2182). The Node SDK's type names only json_object but takes any string, so json_schema rests on the Python SDK and IBM's LangChain integration. IBM's SDK notes that JSON mode needs the prompt to ask for JSON as well (NODE types/vml_v1.ts:3284-3292); this client adds no instruction. Whether a model honours either format depends on the model.
== Reasoning ==
CompletionOptions.reasoning is sent as reasoning_effort (low, medium or high; SDK-evidenced: NODE types/vml_v1.ts:860-864 and vml_v1.ts:2828,3053 for textChat and textChatStream; PY schema/_api.py:289; LCIBM chat_models.py:1010), but only to the models known to take it (WatsonXClient.takesReasoningEffort): OpenAI's gpt-oss models, openai/gpt-oss-*, which IBM's clients send it to (JAVA ChatServiceThinkingTest; LCIBM chat_models.py:657-690). Any other model is sent nothing, and a warning is logged once per process: IBM's Model Gateway rejects the parameter for models without it (JAVA ModelGatewayChatServiceIT), and whether the chat API does is unchecked. The chat API has no value that turns reasoning off, so ReasoningEffort.None is sent as low, the least, with a warning logged once per process (WatsonXClient.reasoningEffortValue). include_reasoning is not sent: it defaults to true (NODE types/vml_v1.ts:858).
A reply's reasoning is read as thinking, from any model: message.reasoning_content on complete (NODE types/vml_v1.ts:3383-3384) and delta.reasoning_content on a stream, passed on as StreamedChunk.thinkingDelta (JAVA gpt_oss_thinking_response.txt and gpt_oss_thinking_streaming_response.txt, recorded replies of openai/gpt-oss-120b); reasoning, which IBM's LangChain integration also reads (LCIBM chat_models.py:150-153, 332-335), when reasoning_content is absent. Thinking is not sent back on a later turn: the chat API's assistant message has no field for it. Reasoning tokens are not reported: the chat API's usage has only prompt_tokens, completion_tokens and total_tokens (NODE types/vml_v1.ts:3496-3503, and the recorded replies above), so TokenUsage.thinkingTokens stays empty. budgetTokens has no counterpart and is ignored.
== Finish reasons and stream endings ==
A stream must end with a choice that carries a finish_reason. One that ends without it, or whose reason is in WatsonXClient.ErrorFinishReasons, is a Left(ServiceError) naming the reason; text received so far is not returned as a success. Every other reason (stop, length, tool_calls, unknown values) is a normal stop. complete applies the same rule to choices[0].finish_reason (a missing one is fine). The usage arrives in a final chunk that may have no choices (SDK-evidenced: LCIBM chat_models.py:379-383). Streamed tool calls are reported once, whole, after the last delta, never as fragments.
An event: error frame ends the stream with IBM's error: its data: line holds IBM's error JSON, whose status_code picks the LLMError as an HTTP status would (401/403 AuthenticationError, 429 RateLimitError, 400 ValidationError, otherwise ServiceError; none is ServiceError(502)) and whose first errors entry's code and message, redacted, are the message (SDK-evidenced: PY foundation_models/inference/fm_model_inference.py:683-689 raises on that frame).
== Timeouts ==
A chat call times out after two minutes and a streamed one after ten, unless the section's timeouts { request = ..., stream = ... } block sets either (WatsonXConfig.timeouts). The IAM token exchange keeps its own 30 seconds.
Value parameters
- config
-
model, credentials, project or space and endpoints.
- exchangeLogging
-
optional provider exchange logging.
- httpClient
-
used for both the model calls and the IAM exchange.
- metrics
-
receives per-call latency and token-usage events.
Attributes
- Companion
- object
- Source
- WatsonXClient.scala
- Graph
-
- Supertypes
-
trait BaseLifecycleLLMClienttrait MetricsRecordingtrait LLMClienttrait AutoCloseableclass Objecttrait Matchableclass Any
