libs/partners/fireworks/langchain_fireworks/chat_models.py PYTHON 2,080 lines View on github.com → Search inside
File is large — showing lines 1–2,000 of 2,080.
1"""Fireworks chat wrapper."""23from __future__ import annotations45import contextlib6import json7import logging8from collections.abc import AsyncIterator, Callable, Iterator, Mapping, Sequence9from contextvars import ContextVar10from operator import itemgetter11from typing import (12    Any,13    Literal,14    NoReturn,15    Self,16    TypeAlias,17    cast,18)1920import httpx21from fireworks import (22    APIConnectionError,23    APIError,24    APITimeoutError,25    AsyncFireworks,26    AuthenticationError,27    BadRequestError,28    Fireworks,29    FireworksError,30    InternalServerError,31    NotFoundError,32    PermissionDeniedError,33    RateLimitError,34)35from langchain_core.callbacks import (36    AsyncCallbackManagerForLLMRun,37    CallbackManagerForLLMRun,38)39from langchain_core.exceptions import (40    ContextOverflowError,41    ModelAPIError,42    ModelAuthenticationError,43    ModelConnectionError,44    ModelInvalidRequestError,45    ModelNotFoundError,46    ModelPermissionDeniedError,47    ModelRateLimitError,48    ModelTimeoutError,49)50from langchain_core.language_models import (51    LanguageModelInput,52    ModelProfile,53    ModelProfileRegistry,54)55from langchain_core.language_models.chat_models import (56    BaseChatModel,57    LangSmithParams,58    agenerate_from_stream,59    generate_from_stream,60)61from langchain_core.language_models.llms import create_base_retry_decorator62from langchain_core.messages import (63    AIMessage,64    AIMessageChunk,65    BaseMessage,66    BaseMessageChunk,67    ChatMessage,68    ChatMessageChunk,69    FunctionMessage,70    FunctionMessageChunk,71    HumanMessage,72    HumanMessageChunk,73    InvalidToolCall,74    SystemMessage,75    SystemMessageChunk,76    ToolCall,77    ToolMessage,78    ToolMessageChunk,79    UsageMetadata,80    is_data_content_block,81)82from langchain_core.messages.block_translators.openai import (83    convert_to_openai_data_block,84)85from langchain_core.messages.tool import (86    ToolCallChunk,87)88from langchain_core.messages.tool import (89    tool_call_chunk as create_tool_call_chunk,90)91from langchain_core.output_parsers import JsonOutputParser, PydanticOutputParser92from langchain_core.output_parsers.base import OutputParserLike93from langchain_core.output_parsers.openai_tools import (94    JsonOutputKeyToolsParser,95    PydanticToolsParser,96    make_invalid_tool_call,97    parse_tool_call,98)99from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult100from langchain_core.runnables import Runnable, RunnableMap, RunnablePassthrough101from langchain_core.tools import BaseTool102from langchain_core.utils import (103    get_pydantic_field_names,104)105from langchain_core.utils._gateway import _apply_gateway_config106from langchain_core.utils.function_calling import (107    convert_to_json_schema,108    convert_to_openai_tool,109)110from langchain_core.utils.pydantic import is_basemodel_subclass111from langchain_core.utils.utils import _build_model_kwargs112from pydantic import (113    BaseModel,114    ConfigDict,115    Field,116    PrivateAttr,117    SecretStr,118    model_validator,119)120121from langchain_fireworks._compat import _convert_from_v1_to_chat_completions122from langchain_fireworks._version import __version__123from langchain_fireworks.data._profiles import _PROFILES124125logger = logging.getLogger(__name__)126127_PROMPT_CACHE_AFFINITY: ContextVar[str | None] = ContextVar(128    "fireworks_prompt_cache_affinity", default=None129)130131132_MODEL_PROFILES = cast("ModelProfileRegistry", _PROFILES)133134135def _get_default_model_profile(model_name: str) -> ModelProfile:136    default = _MODEL_PROFILES.get(model_name) or {}137    return default.copy()138139140def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:141    """Convert a dictionary to a LangChain message.142143    Args:144        _dict: The dictionary.145146    Returns:147        The LangChain message.148149    """150    role = _dict.get("role")151    if role == "user":152        return HumanMessage(content=_dict.get("content", ""))153    if role == "assistant":154        # Fix for azure155        # Also Fireworks returns None for tool invocations156        content = _dict.get("content", "") or ""157        additional_kwargs: dict = {}158        if (159            isinstance(reasoning_content := _dict.get("reasoning_content"), str)160            and reasoning_content161        ):162            additional_kwargs["reasoning_content"] = reasoning_content163164        if function_call := _dict.get("function_call"):165            additional_kwargs["function_call"] = dict(function_call)166167        tool_calls = []168        invalid_tool_calls = []169        if raw_tool_calls := _dict.get("tool_calls"):170            additional_kwargs["tool_calls"] = raw_tool_calls171            for raw_tool_call in raw_tool_calls:172                try:173                    tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))174                except Exception as e:175                    invalid_tool_calls.append(176                        dict(make_invalid_tool_call(raw_tool_call, str(e)))177                    )178        return AIMessage(179            content=content,180            additional_kwargs=additional_kwargs,181            tool_calls=tool_calls,182            invalid_tool_calls=invalid_tool_calls,183        )184    if role == "system":185        return SystemMessage(content=_dict.get("content", ""))186    if role == "function":187        return FunctionMessage(188            content=_dict.get("content", ""), name=_dict.get("name", "")189        )190    if role == "tool":191        additional_kwargs = {}192        if "name" in _dict:193            additional_kwargs["name"] = _dict["name"]194        return ToolMessage(195            content=_dict.get("content", ""),196            tool_call_id=_dict.get("tool_call_id", ""),197            additional_kwargs=additional_kwargs,198        )199    return ChatMessage(content=_dict.get("content", ""), role=role or "")200201202def _allowed_content_part_keys() -> frozenset[str]:203    """Allowlist of wire-valid keys on a Fireworks content part.204205    Derived at import time from the stainless-generated TypedDict so the206    allowlist tracks the upstream OpenAPI spec as `fireworks-ai` is bumped:207    new fields widen the allowlist for free, removed/renamed fields shrink it208    in lockstep. If the SDK reshuffles its module layout the import falls back209    to a conservative hand-coded set and emits a warning, and the layout test210    (`test_fireworks_sdk_request_layout_stable`) fails to surface the drift.211    """212    try:213        from typing import get_type_hints214215        from fireworks.types.shared_params.chat_message import (216            ContentUnionMember1,217        )218219        return frozenset(get_type_hints(ContentUnionMember1))220    except ImportError:221        logger.warning(222            "Could not import `fireworks.types.shared_params.chat_message."223            "ContentUnionMember1`; falling back to a conservative content-part "224            "key allowlist. Bump `fireworks-ai` or update "225            "`_allowed_content_part_keys` if the SDK has moved this type.",226        )227        return frozenset({"type", "text", "image_url", "video_url"})228229230_ALLOWED_CONTENT_PART_KEYS: frozenset[str] = _allowed_content_part_keys()231232233_DROPPED_CONTENT_BLOCK_TYPES: frozenset[str] = frozenset(234    {235        "tool_use",236        "thinking",237        "reasoning",238        "reasoning_content",239        "function_call",240        "code_interpreter_call",241    }242)243"""Content block types the chat completions wire format does not carry.244245These arise from provider-specific or canonical v1 content (e.g. Anthropic246`tool_use`/`thinking` blocks, or a `reasoning` block on an AIMessage) that247reaches `_convert_message_to_dict` as conversation history. Fireworks rejects248them, so they are dropped rather than forwarded.249"""250251252def _sanitize_chat_completions_content(content: Any) -> Any:253    """Strip non-wire keys from content blocks before serializing to Fireworks.254255    Fireworks's chat completions endpoint rejects unknown fields on message256    content parts with `Extra inputs are not permitted, field: 'messages[N]257    .content.list[ChatMessageContent][i].<key>'`. This surfaces when a258    conversation accumulates AIMessages from a different provider (e.g.259    Anthropic's v1 streaming-reassembly `index` marker on text blocks, or the260    LangChain-internal `caller` key on `tool_use` blocks) and that history is261    later forwarded to a Fireworks-hosted model.262263    For list content:264        - each block dict is filtered down to keys in265            `_ALLOWED_CONTENT_PART_KEYS` (sourced from the SDK TypedDict, so it266            stays in sync with the upstream spec).267        - if the result is a list of exactly one block that, post-strip, is268            `{"type": "text", "text": <str>}` and nothing else, it is coerced to269            a plain string. Fireworks's `content` union lists `str` first270            (`Input should be a valid string, field: 'messages[N].content.str'`),271            and the stricter shape avoids the union-validation noise on the272            server side.273    Non-list content (strings, None) passes through unchanged.274    """275    if not isinstance(content, list):276        return content277    sanitized: list[Any] = []278    for block in content:279        if isinstance(block, dict):280            sanitized.append(281                {k: v for k, v in block.items() if k in _ALLOWED_CONTENT_PART_KEYS}282            )283        else:284            sanitized.append(block)285    if (286        len(sanitized) == 1287        and isinstance(sanitized[0], dict)288        and set(sanitized[0]) == {"type", "text"}289        and sanitized[0]["type"] == "text"290        and isinstance(sanitized[0]["text"], str)291    ):292        return sanitized[0]["text"]293    return sanitized294295296def _format_message_content(content: Any) -> Any:297    """Format message content for the Fireworks chat completions wire format.298299    Adapted from `langchain_openai.chat_models.base._format_message_content`,300    scoped to the chat completions API: drops content block types the wire301    format does not carry, translates canonical v0/v1 multimodal data blocks302    via `convert_to_openai_data_block(block, api="chat/completions")`, and303    converts legacy Anthropic-shape image blocks (`{"type": "image",304    "source": {...}}`) to OpenAI `image_url` blocks. String and non-list305    content are returned unchanged.306307    Args:308        content: The message content. Strings and non-list values are309            returned as-is; lists are walked block by block.310311    Returns:312        The formatted content, ready to be placed on the chat completions313        wire. List inputs return a new list with translations applied; other314        inputs are returned unchanged.315    """316    if not isinstance(content, list):317        return content318    formatted: list[Any] = []319    for block in content:320        if isinstance(block, dict) and "type" in block:321            btype = block["type"]322            if btype in _DROPPED_CONTENT_BLOCK_TYPES:323                continue324            if is_data_content_block(block):325                formatted.append(326                    convert_to_openai_data_block(block, api="chat/completions")327                )328                continue329            if (330                btype == "image"331                and (source := block.get("source"))332                and isinstance(source, dict)333            ):334                if (335                    source.get("type") == "base64"336                    and (media_type := source.get("media_type"))337                    and (data := source.get("data"))338                ):339                    formatted.append(340                        {341                            "type": "image_url",342                            "image_url": {"url": f"data:{media_type};base64,{data}"},343                        }344                    )345                    continue346                if source.get("type") == "url" and (url := source.get("url")):347                    formatted.append({"type": "image_url", "image_url": {"url": url}})348                    continue349                continue350        formatted.append(block)351    return formatted352353354def _format_tool_call_arguments(arguments: str | dict | None) -> str | dict:355    """Preserve invalid historical arguments inside a JSON object for replay."""356    if isinstance(arguments, dict):357        return arguments358    try:359        parsed = json.loads(arguments) if arguments is not None else None360        if isinstance(parsed, dict) and arguments is not None:361            json.dumps(parsed, allow_nan=False)362            return arguments363    except ValueError:364        logger.debug("Invalid JSON in historical Fireworks tool call arguments")365    logger.warning("Wrapping invalid historical Fireworks tool call arguments")366    return json.dumps({"__invalid_tool_call_arguments": arguments}, ensure_ascii=False)367368369def _convert_message_to_dict(message: BaseMessage) -> dict:370    """Convert a LangChain message to a dictionary.371372    Args:373        message: The LangChain message.374375    Returns:376        The dictionary.377378    """379    message_dict: dict[str, Any]380    if isinstance(message, ChatMessage):381        message_dict = {382            "role": message.role,383            "content": _sanitize_chat_completions_content(384                _format_message_content(message.content)385            ),386        }387    elif isinstance(message, HumanMessage):388        message_dict = {389            "role": "user",390            "content": _sanitize_chat_completions_content(391                _format_message_content(message.content)392            ),393        }394    elif isinstance(message, AIMessage):395        # Translate v1 content396        if message.response_metadata.get("output_version") == "v1":397            message = _convert_from_v1_to_chat_completions(message)398        message_dict = {399            "role": "assistant",400            "content": _sanitize_chat_completions_content(401                _format_message_content(message.content)402            ),403        }404        # Replay reasoning only when the message is tagged `fireworks` or has no405        # `model_provider` (hand-built messages, or integrations that don't tag406        # theirs); reasoning from other tagged providers may not be valid input.407        if isinstance(408            reasoning_content := message.additional_kwargs.get("reasoning_content"), str409        ) and message.response_metadata.get("model_provider") in (None, "fireworks"):410            message_dict["reasoning_content"] = reasoning_content411        if "function_call" in message.additional_kwargs:412            message_dict["function_call"] = message.additional_kwargs["function_call"]413            # If function call only, content is None not empty string414            if message_dict["content"] == "":415                message_dict["content"] = None416        if message.tool_calls or message.invalid_tool_calls:417            message_dict["tool_calls"] = [418                _lc_tool_call_to_fireworks_tool_call(tc) for tc in message.tool_calls419            ] + [420                _lc_invalid_tool_call_to_fireworks_tool_call(tc)421                for tc in message.invalid_tool_calls422            ]423        elif "tool_calls" in message.additional_kwargs:424            message_dict["tool_calls"] = message.additional_kwargs["tool_calls"]425        if "tool_calls" in message_dict:426            message_dict["tool_calls"] = [427                {428                    **tool_call,429                    "function": {430                        **tool_call["function"],431                        "arguments": _format_tool_call_arguments(432                            tool_call["function"]["arguments"]433                        ),434                    },435                }436                for tool_call in message_dict["tool_calls"]437            ]438        # If tool calls only, content is None not empty string439        if "tool_calls" in message_dict and message_dict["content"] == "":440            message_dict["content"] = None441        else:442            pass443    elif isinstance(message, SystemMessage):444        message_dict = {445            "role": "system",446            "content": _sanitize_chat_completions_content(447                _format_message_content(message.content)448            ),449        }450    elif isinstance(message, FunctionMessage):451        message_dict = {452            "role": "function",453            "content": message.content,454            "name": message.name,455        }456    elif isinstance(message, ToolMessage):457        message_dict = {458            "role": "tool",459            "content": _sanitize_chat_completions_content(460                _format_message_content(message.content)461            ),462            "tool_call_id": message.tool_call_id,463        }464    else:465        msg = f"Got unknown type {message}"466        raise TypeError(msg)467    if "name" in message.additional_kwargs:468        message_dict["name"] = message.additional_kwargs["name"]469    return message_dict470471472def _usage_to_metadata(usage: Mapping[str, Any]) -> UsageMetadata:473    input_tokens = usage.get("prompt_tokens") or 0474    output_tokens = usage.get("completion_tokens") or 0475    usage_metadata: UsageMetadata = {476        "input_tokens": input_tokens,477        "output_tokens": output_tokens,478        "total_tokens": usage.get("total_tokens") or input_tokens + output_tokens,479    }480    cached_tokens = (usage.get("prompt_tokens_details") or {}).get("cached_tokens")481    if cached_tokens is not None:482        usage_metadata["input_token_details"] = {"cache_read": cached_tokens}483    reasoning_tokens = (usage.get("completion_tokens_details") or {}).get(484        "reasoning_tokens"485    )486    if reasoning_tokens is not None:487        usage_metadata["output_token_details"] = {"reasoning": reasoning_tokens}488    return usage_metadata489490491TokenUsageTree: TypeAlias = "int | dict[str, TokenUsageTree]"492"""Raw provider token usage: a tree of `int` leaves and nested `dict` nodes493(e.g. `prompt_tokens_details`).494495Modeled as a recursive alias so the merge helper's signature carries the shape496rather than leaving it to `Any`.497"""498499500def _update_token_usage(501    overall_token_usage: TokenUsageTree, new_usage: TokenUsageTree502) -> TokenUsageTree:503    """Recursively merge raw provider token usage across generations.504505    Token usage is a tree of `int` leaves (summed) and `dict` nodes such as506    `prompt_tokens_details` (merged key-by-key, skipping `None` values).507508    A type mismatch between the accumulator and the incoming value (e.g. an509    `int` on one side and a `dict` on the other) indicates malformed provider510    data and is raised rather than silently coerced. An entirely unexpected511    leaf type (neither `int` nor `dict`) is logged and passed through, so a512    telemetry anomaly degrades gracefully instead of failing the response.513    """514    if isinstance(new_usage, int):515        if not isinstance(overall_token_usage, int):516            msg = (517                "Got different types for token usage: "518                f"{new_usage!r} ({type(new_usage).__name__}) and "519                f"{overall_token_usage!r} ({type(overall_token_usage).__name__})"520            )521            raise ValueError(msg)522        return overall_token_usage + new_usage523    if isinstance(new_usage, dict):524        if not isinstance(overall_token_usage, dict):525            msg = (526                "Got different types for token usage: "527                f"{new_usage!r} ({type(new_usage).__name__}) and "528                f"{overall_token_usage!r} ({type(overall_token_usage).__name__})"529            )530            raise ValueError(msg)531        updated_token_usage = dict(overall_token_usage)532        for key, value in new_usage.items():533            if value is not None:534                # Seed a first-seen key with an empty node of the same kind so a535                # nested `dict` value merges rather than colliding with an `int`.536                default: TokenUsageTree = {} if isinstance(value, dict) else 0537                updated_token_usage[key] = _update_token_usage(538                    overall_token_usage.get(key, default), value539                )540        return updated_token_usage541    logger.warning("Unexpected type for token usage: %s", type(new_usage).__name__)542    return new_usage543544545def _convert_chunk_to_message_chunk(546    chunk: Mapping[str, Any], default_class: type[BaseMessageChunk]547) -> BaseMessageChunk:548    choices = chunk.get("choices") or []549    response_metadata: dict[str, Any] = {"model_provider": "fireworks"}550    if not choices:551        # Final chunk emitted when `stream_options.include_usage=True`:552        # `choices` is empty and the chunk carries only `usage`.553        usage = chunk.get("usage")554        if not usage:555            logger.debug(556                "Received stream chunk with no choices and no usage: %s", chunk557            )558        usage_metadata = _usage_to_metadata(usage) if usage else None559        return AIMessageChunk(560            content="",561            usage_metadata=usage_metadata,562            response_metadata=response_metadata,563        )564    choice = choices[0]565    _dict = choice["delta"]566    role = cast(str, _dict.get("role"))567    content = cast(str, _dict.get("content") or "")568    additional_kwargs: dict = {}569    tool_call_chunks: list[ToolCallChunk] = []570    if (571        isinstance(reasoning_content := _dict.get("reasoning_content"), str)572        and reasoning_content573    ):574        additional_kwargs["reasoning_content"] = reasoning_content575    if _dict.get("function_call"):576        function_call = dict(_dict["function_call"])577        if "name" in function_call and function_call["name"] is None:578            function_call["name"] = ""579        additional_kwargs["function_call"] = function_call580    if raw_tool_calls := _dict.get("tool_calls"):581        additional_kwargs["tool_calls"] = raw_tool_calls582        for rtc in raw_tool_calls:583            with contextlib.suppress(KeyError):584                tool_call_chunks.append(585                    create_tool_call_chunk(586                        name=rtc["function"].get("name"),587                        args=rtc["function"].get("arguments"),588                        id=rtc.get("id"),589                        index=rtc.get("index"),590                    )591                )592    if role == "user" or default_class == HumanMessageChunk:593        return HumanMessageChunk(content=content)594    if role == "assistant" or default_class == AIMessageChunk:595        usage = chunk.get("usage")596        usage_metadata = _usage_to_metadata(usage) if usage else None597        return AIMessageChunk(598            content=content,599            additional_kwargs=additional_kwargs,600            tool_call_chunks=tool_call_chunks,601            usage_metadata=usage_metadata,602            response_metadata=response_metadata,603        )604    if role == "system" or default_class == SystemMessageChunk:605        return SystemMessageChunk(content=content)606    if role == "function" or default_class == FunctionMessageChunk:607        return FunctionMessageChunk(content=content, name=_dict["name"])608    if role == "tool" or default_class == ToolMessageChunk:609        return ToolMessageChunk(content=content, tool_call_id=_dict["tool_call_id"])610    if role or default_class == ChatMessageChunk:611        return ChatMessageChunk(content=content, role=role)612    return default_class(content=content)  # type: ignore[call-arg]613614615def _format_chunk_for_v1(message_chunk: AIMessageChunk) -> AIMessageChunk:616    """Convert a stream chunk to v1 content blocks with separate index namespaces.617618    Fireworks streams a single text field and a single `reasoning_content` field,619    but numbers tool calls from 0. Left to core, streaming (`stream`, `astream`,620    and streaming `invoke`) gives unindexed blocks an integer that increments each621    time the block type changes, so tool call 0 would be merged into the reasoning622    block by `merge_lists`, and reasoning interrupted by text would split in two.623624    `merge_lists` merges blocks with matching indices only when the index is an int625    or a string starting with `lc_`; other strings are appended unmerged. The raw626    `tool_call_chunks` keep the provider's integer indices, which627    `AIMessageChunk.__add__` relies on to merge tool-call arguments.628    """629    blocks: list[dict[str, Any]] = []630    if reasoning := message_chunk.additional_kwargs.get("reasoning_content"):631        blocks.append(632            {"type": "reasoning", "reasoning": reasoning, "index": "lc_reasoning"}633        )634    if message_chunk.content:635        blocks.append(636            {"type": "text", "text": message_chunk.content, "index": "lc_text"}637        )638    for tool_call_chunk in message_chunk.tool_call_chunks:639        block: dict[str, Any] = {640            "type": "tool_call_chunk",641            "id": tool_call_chunk["id"],642            "name": tool_call_chunk["name"],643            "args": tool_call_chunk["args"],644        }645        if (index := tool_call_chunk["index"]) is not None:646            block["index"] = f"lc_tc_{index}"647        blocks.append(block)648    return message_chunk.model_copy(649        update={650            "content": blocks,651            "response_metadata": {652                **message_chunk.response_metadata,653                "output_version": "v1",654            },655        }656    )657658659def _record_usage(previous: UsageMetadata | None, new: UsageMetadata) -> UsageMetadata:660    """Return the newest usage report, logging when it disagrees with an earlier one."""661    if previous is not None and previous != new:662        logger.debug(663            "Fireworks reported differing usage within one stream; using the "664            "latest. Previous: %s, latest: %s",665            previous,666            new,667        )668    return new669670671def _finalize_stream(672    stream: Iterator[ChatGenerationChunk],673) -> Iterator[ChatGenerationChunk]:674    """Supply core's finalization marker when aggregating v1 chunks internally."""675    yield from stream676    yield ChatGenerationChunk(message=AIMessageChunk(content="", chunk_position="last"))677678679async def _afinalize_stream(680    stream: AsyncIterator[ChatGenerationChunk],681) -> AsyncIterator[ChatGenerationChunk]:682    """Supply core's finalization marker when aggregating v1 chunks internally."""683    async for chunk in stream:684        yield chunk685    yield ChatGenerationChunk(message=AIMessageChunk(content="", chunk_position="last"))686687688class _RetryableHTTPStatusError(FireworksError):689    """Internal marker for 5xx `httpx.HTTPStatusError` responses.690691    The 1.x SDK wraps every status response into a typed `APIStatusError`692    subclass, so this path is defense-in-depth: it only fires when a raw693    `httpx.HTTPStatusError` escapes the SDK (e.g., a custom `http_client` or694    monkey-patched transport raises one directly). Promoting it here keeps the695    retryable set expressible as a list of classes for696    `create_base_retry_decorator`.697    """698699700_RETRYABLE_ERRORS: tuple[type[BaseException], ...] = (701    APIConnectionError,702    InternalServerError,703    RateLimitError,704    httpx.TimeoutException,705    httpx.TransportError,706    _RetryableHTTPStatusError,707)708709710def _promote_http_status_error(exc: httpx.HTTPStatusError) -> NoReturn:711    """Re-raise 5xx `httpx.HTTPStatusError` as a retryable marker."""712    if exc.response.status_code >= 500:713        msg = f"Retryable {exc.response.status_code} from Fireworks: {exc}"714        raise _RetryableHTTPStatusError(msg) from exc715    raise exc716717718class FireworksContextOverflowError(BadRequestError, ContextOverflowError):719    """`BadRequestError` raised when input exceeds Fireworks's context limit."""720721722class FireworksAuthenticationError(AuthenticationError, ModelAuthenticationError):723    """Fireworks authentication error classified as a LangChain model error."""724725726class FireworksPermissionDeniedError(PermissionDeniedError, ModelPermissionDeniedError):727    """Fireworks permission error classified as a LangChain model error."""728729730class FireworksInvalidRequestError(BadRequestError, ModelInvalidRequestError):731    """Fireworks bad-request error classified as a LangChain model error."""732733734class FireworksModelNotFoundError(NotFoundError, ModelNotFoundError):735    """Fireworks not-found error classified as a LangChain model error."""736737738class FireworksRateLimitError(RateLimitError, ModelRateLimitError):739    """Fireworks rate-limit error classified as a LangChain model error."""740741742class FireworksAPIError(InternalServerError, ModelAPIError):743    """Fireworks server error classified as a LangChain model error."""744745746class FireworksConnectionError(APIConnectionError, ModelConnectionError):747    """Fireworks connection error classified as a LangChain model error."""748749750class FireworksTimeoutError(APITimeoutError, ModelTimeoutError):751    """Fireworks timeout error classified as a LangChain model error."""752753754class FireworksReadTimeoutError(httpx.ReadTimeout, ModelTimeoutError):755    """Fireworks stream read timeout classified as a retryable model error."""756757758def _handle_stream_read_timeout(error: httpx.ReadTimeout) -> NoReturn:759    try:760        request = error.request761    except RuntimeError:762        request = None763    raise FireworksReadTimeoutError(str(error), request=request) from error764765766def _handle_fireworks_invalid_request(e: BadRequestError) -> NoReturn:767    """Promote prompt-too-long errors to `FireworksContextOverflowError`."""768    if "prompt is too long" in str(e):769        raise FireworksContextOverflowError(770            str(e), response=e.response, body=e.body771        ) from e772    raise FireworksInvalidRequestError(str(e), response=e.response, body=e.body) from e773774775def _handle_fireworks_api_error(e: APIError) -> NoReturn:776    """Re-raise a Fireworks SDK error as its LangChain-classified equivalent."""777    if isinstance(e, AuthenticationError):778        raise FireworksAuthenticationError(779            str(e), response=e.response, body=e.body780        ) from e781    if isinstance(e, PermissionDeniedError):782        raise FireworksPermissionDeniedError(783            str(e), response=e.response, body=e.body784        ) from e785    if isinstance(e, NotFoundError):786        raise FireworksModelNotFoundError(787            str(e), response=e.response, body=e.body788        ) from e789    if isinstance(e, RateLimitError):790        raise FireworksRateLimitError(str(e), response=e.response, body=e.body) from e791    if isinstance(e, InternalServerError):792        raise FireworksAPIError(str(e), response=e.response, body=e.body) from e793    # `APITimeoutError` subclasses `APIConnectionError`, so check it first.794    if isinstance(e, APITimeoutError):795        raise FireworksTimeoutError(e.request) from e796    if isinstance(e, APIConnectionError):797        raise FireworksConnectionError(message=str(e), request=e.request) from e798    raise e799800801def _raise_empty_stream() -> NoReturn:802    """Raise a descriptive error when the SDK returns a zero-chunk stream."""803    msg = "Received empty stream from Fireworks"804    raise FireworksError(msg)805806807def _create_retry_decorator(808    llm: ChatFireworks,809    run_manager: AsyncCallbackManagerForLLMRun | CallbackManagerForLLMRun | None = None,810) -> Callable[[Any], Any]:811    """Return a tenacity retry decorator for Fireworks SDK calls.812813    Retries live here rather than in the SDK so each attempt is visible to the814    LangChain `run_manager.on_retry` callback. The SDK's own retry layer is815    suppressed via `max_retries=0` on the client; see `validate_environment`.816    """817    # `max_retries` counts retries *after* the initial attempt (default lives on818    # the `ChatFireworks.max_retries` field). `create_base_retry_decorator`819    # forwards its `max_retries` to `stop_after_attempt`, which counts total820    # attempts — so offset by 1. `None` and `0` both mean "single attempt, no821    # retries".822    attempts = (llm.max_retries + 1) if llm.max_retries else 1823    return create_base_retry_decorator(824        error_types=list(_RETRYABLE_ERRORS),825        max_retries=attempts,826        run_manager=run_manager,827    )828829830def _prepare_sdk_kwargs(kwargs: dict[str, Any]) -> dict[str, Any]:831    """Move fields the 1.x SDK does not model into `extra_body`.832833    The Stainless-generated `chat.completions.create` signature has a fixed set834    of typed parameters. Fireworks accepts additional fields on the wire (notably835    `stream_options.include_usage`) that the SDK schema does not declare. The836    SDK exposes `extra_body` precisely for this — merge anything that looks837    extra-body-shaped into it so it lands in the JSON request body.838839    If a caller supplies both `extra_body={"stream_options": ...}` and a840    top-level `stream_options=...`, the value already in `extra_body` wins841    (callers using `extra_body` are presumed to want explicit control); the842    discarded top-level value is logged.843    """844    if "max_completion_tokens" in kwargs:845        kwargs.pop("max_tokens", None)846    extra_body = dict(kwargs.pop("extra_body", None) or {})847    top_level_stream_options = kwargs.pop("stream_options", None)848    if top_level_stream_options is not None:849        if "stream_options" in extra_body:850            logger.warning(851                "Both `extra_body['stream_options']` and a top-level "852                "`stream_options` were supplied; using `extra_body`'s value "853                "and discarding the top-level value.",854            )855        else:856            extra_body["stream_options"] = top_level_stream_options857    if extra_body:858        kwargs["extra_body"] = extra_body859    return kwargs860861862def _merge_model_headers(llm: ChatFireworks, kwargs: dict[str, Any]) -> dict[str, Any]:863    """Merge model-local headers only when invoking their owning model."""864    model_headers = llm.model_kwargs.get("extra_headers")865    request_headers = kwargs.get("extra_headers")866    if isinstance(model_headers, Mapping) and isinstance(request_headers, Mapping):867        # HTTP header names are case-insensitive; retain the request's spelling.868        headers = {869            key.lower(): (key, value)870            for source in (model_headers, request_headers)871            for key, value in source.items()872        }873        return {874            **kwargs,875            "extra_headers": dict(headers.values()),876        }877    return kwargs878879880def _apply_prompt_cache_affinity(kwargs: dict[str, Any]) -> dict[str, Any]:881    """Apply the middleware default after the selected model's settings are merged."""882    affinity = _PROMPT_CACHE_AFFINITY.get()883    if affinity is None:884        return kwargs885886    # The SDK merges extra_body over top-level fields before sending the request.887    extra_body = kwargs.get("extra_body")888    body = {**kwargs, **extra_body} if isinstance(extra_body, Mapping) else kwargs889    if any(body.get(key) for key in ("user", "prompt_cache_key")):890        return kwargs891892    headers = kwargs.get("extra_headers")893    if isinstance(headers, Mapping):894        if any(895            isinstance(key, str) and key.lower() == "x-session-affinity"896            for key in headers897        ):898            return kwargs899    elif headers is not None:900        logger.warning(901            "Cannot set Fireworks session affinity because extra_headers is %s",902            type(headers).__name__,903        )904        return kwargs905906    return {907        **kwargs,908        "prompt_cache_key": affinity,909        "extra_headers": {**(headers or {}), "x-session-affinity": affinity},910    }911912913def _completion_with_retry(914    llm: ChatFireworks,915    run_manager: CallbackManagerForLLMRun | None = None,916    **kwargs: Any,917) -> Any:918    """Retry the sync completion call, including stream setup."""919    retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)920    kwargs = _prepare_sdk_kwargs(921        _apply_prompt_cache_affinity(_merge_model_headers(llm, kwargs))922    )923924    @retry_decorator925    def _call() -> Any:926        try:927            result = llm.client.create(**kwargs)928        except httpx.HTTPStatusError as e:929            _promote_http_status_error(e)930        if kwargs.get("stream"):931            # The streaming generator is lazy — advance once so the HTTP932            # connection and any transport error happen inside the retry933            # boundary. `_prepend_chunk` then re-yields the consumed chunk934            # ahead of the rest so callers still see every event.935            try:936                iterator = iter(result)937                first = next(iterator)938            except StopIteration:939                _raise_empty_stream()940            except httpx.HTTPStatusError as e:941                _promote_http_status_error(e)942            return _prepend_chunk(first, iterator)943        return result944945    return _call()946947948async def _acompletion_with_retry(949    llm: ChatFireworks,950    run_manager: AsyncCallbackManagerForLLMRun | None = None,951    **kwargs: Any,952) -> Any:953    """Retry the async completion call, including stream setup."""954    retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)955    kwargs = _prepare_sdk_kwargs(956        _apply_prompt_cache_affinity(_merge_model_headers(llm, kwargs))957    )958959    @retry_decorator960    async def _call() -> Any:961        if kwargs.get("stream"):962            try:963                # 1.x async `create()` is a coroutine that resolves to an964                # `AsyncStream` when `stream=True`. Await it, then advance the965                # async iterator once inside the retry boundary so transport966                # errors surface here rather than at first downstream consumer.967                result = await llm.async_client.create(**kwargs)968                agen = result.__aiter__()969                first = await agen.__anext__()970            except StopAsyncIteration:971                _raise_empty_stream()972            except httpx.HTTPStatusError as e:973                _promote_http_status_error(e)974            return _aprepend_chunk(first, agen)975        try:976            return await llm.async_client.create(**kwargs)977        except httpx.HTTPStatusError as e:978            _promote_http_status_error(e)979980    return await _call()981982983def _prepend_chunk(first: Any, rest: Iterator[Any]) -> Iterator[Any]:984    yield first985    try:986        yield from rest987    except httpx.ReadTimeout as e:988        _handle_stream_read_timeout(e)989990991async def _aprepend_chunk(first: Any, rest: AsyncIterator[Any]) -> AsyncIterator[Any]:992    yield first993    try:994        async for item in rest:995            yield item996    except httpx.ReadTimeout as e:997        _handle_stream_read_timeout(e)9989991000class ChatFireworks(BaseChatModel):1001    """`Fireworks` Chat large language models API.10021003    To use, you should have the1004    environment variable `FIREWORKS_API_KEY` set with your API key.10051006    Any parameters that are valid to be passed to the fireworks.create call1007    can be passed in, even if not explicitly saved on this class.10081009    Example:1010        ```python1011        from langchain_fireworks.chat_models import ChatFireworks10121013        model = ChatFireworks(model_name="accounts/fireworks/models/gpt-oss-120b")1014        ```10151016    Fireworks request headers can be passed with `extra_headers`. For prompt1017    caching, `x-session-affinity` pins requests to a replica so related calls can1018    reuse the same prompt-cache session:10191020    ```python1021    model.invoke(1022        "Hello",1023        extra_headers={"x-session-affinity": "user-42"},1024    )1025    ```10261027    The Fireworks SDK also accepts a typed `prompt_cache_key` field (passed as a1028    regular keyword argument), which it treats as the preferred alternative to1029    the raw `x-session-affinity` header:10301031    ```python1032    model.invoke("Hello", prompt_cache_key="user-42")1033    ```1034    """10351036    @property1037    def lc_secrets(self) -> dict[str, str]:1038        return {"fireworks_api_key": "FIREWORKS_API_KEY"}10391040    @classmethod1041    def get_lc_namespace(cls) -> list[str]:1042        """Get the namespace of the LangChain object.10431044        Returns:1045            `["langchain", "chat_models", "fireworks"]`1046        """1047        return ["langchain", "chat_models", "fireworks"]10481049    @property1050    def lc_attributes(self) -> dict[str, Any]:1051        attributes: dict[str, Any] = {}1052        if self.fireworks_api_base:1053            attributes["fireworks_api_base"] = self.fireworks_api_base10541055        return attributes10561057    @classmethod1058    def is_lc_serializable(cls) -> bool:1059        """Return whether this model can be serialized by LangChain."""1060        return True10611062    client: Any = Field(default=None, exclude=True)1063    """Internal `fireworks.Fireworks().chat.completions` resource.10641065    Constructed with `max_retries=0` so retries are owned by1066    `_create_retry_decorator` (which surfaces each attempt to the LangChain1067    `run_manager`). Callers reaching for this directly should set their own1068    retry layer.1069    """10701071    async_client: Any = Field(default=None, exclude=True)1072    """Internal `fireworks.AsyncFireworks().chat.completions` resource.10731074    Constructed with `max_retries=0`; see `client`.1075    """10761077    _sdk_client: Any = PrivateAttr(default=None)1078    """Owning `fireworks.Fireworks` instance, retained so `close()` can call1079    into the underlying HTTPX client. The 1.x SDK does not expose lifecycle1080    methods on the `chat.completions` resource itself.1081    """10821083    _async_sdk_client: Any = PrivateAttr(default=None)1084    """Owning `fireworks.AsyncFireworks` instance; see `_sdk_client`."""10851086    model_name: str = Field(alias="model")1087    """Model name to use."""10881089    @property1090    def model(self) -> str:1091        """Same as model_name."""1092        return self.model_name10931094    temperature: float | None = None1095    """What sampling temperature to use."""10961097    stop: str | list[str] | None = Field(default=None, alias="stop_sequences")1098    """Default stop sequences."""10991100    model_kwargs: dict[str, Any] = Field(default_factory=dict)1101    """Holds any model parameters valid for `create` call not explicitly specified."""11021103    fireworks_api_key: SecretStr = Field(default=SecretStr(""), alias="api_key")1104    """Fireworks API key.11051106    Automatically read from env variable `FIREWORKS_API_KEY` if not provided.11071108    If `LANGSMITH_GATEWAY` is enabled and the base URL points at the gateway,1109    `LANGSMITH_GATEWAY_API_KEY` is used instead.1110    """11111112    fireworks_api_base: str | None = Field(default=None, alias="base_url")1113    """Base URL path for API requests, leave blank if not using a proxy or service1114    emulator.11151116    If `LANGSMITH_GATEWAY` is set, it is used as a fallback after `FIREWORKS_API_BASE`.1117    """11181119    request_timeout: float | tuple[float, float] | Any | None = Field(1120        default=None, alias="timeout"1121    )1122    """Timeout for requests to Fireworks completion API. Can be `float`,1123    `httpx.Timeout` or `None`.1124    """11251126    streaming: bool = False1127    """Whether to stream the results or not."""11281129    stream_usage: bool = True1130    """Whether to include usage metadata in streaming output.11311132    If `True`, a final empty-content chunk carrying `usage_metadata` is emitted1133    during the stream. Set to `False` if the upstream model/proxy rejects1134    `stream_options`, or pass `stream_options` explicitly via `model_kwargs` or1135    a runtime kwarg to override.11361137    !!! version-added "Added in `langchain-fireworks` 1.2.0"11381139    !!! warning "Behavior changed in `langchain-fireworks` 1.2.0"11401141        Streaming now opts into `stream_options.include_usage` by default, and1142        the final empty-`choices` chunk is surfaced as an `AIMessageChunk` with1143        `usage_metadata` instead of being silently dropped.1144    """11451146    n: int = 11147    """Number of chat completions to generate for each prompt."""11481149    max_tokens: int | None = None1150    """Maximum tokens to generate; defaults to `max_output_tokens` in the profile."""11511152    max_retries: int | None = 21153    """Maximum number of retries after the initial attempt when generating.11541155    Retries use exponential backoff and trigger on transient errors:1156    `RateLimitError`, `APIConnectionError` (including its `APITimeoutError`1157    subclass), 5xx responses (including those that surface as1158    `httpx.HTTPStatusError` rather than typed SDK errors), and underlying1159    transport errors (`httpx.TimeoutException`, `httpx.TransportError`).1160    A value of `None` or `0` disables retries.1161    """11621163    service_tier: str | None = None1164    """Service tier for the request.11651166    Forwarded as the `service_tier` field on the Fireworks chat completions1167    request when set. Pass `'priority'` to opt into Fireworks' priority tier;1168    leave as `None` to use the default tier.11691170    To use Fireworks' fast mode instead, select a fast-routed `model`; fast mode1171    is not controlled by this field. See Fireworks'1172    [serverless product docs](https://docs.fireworks.ai/guides/serverless-products)1173    for the current list of fast routers and tiers.11741175    !!! version-added "Added in `langchain-fireworks` 1.3.0"1176    """1177    reasoning_effort: str | None = None1178    """Reasoning effort.11791180    Forwarded as the `reasoning_effort` request field. Supported values vary by1181    model; see the model's `profile.reasoning_effort_levels`.11821183    Can also be passed at call time, e.g.1184    `model.invoke(..., reasoning_effort="high")`.1185    """11861187    model_config = ConfigDict(1188        populate_by_name=True,1189    )11901191    @model_validator(mode="before")1192    @classmethod1193    def build_extra(cls, values: dict[str, Any]) -> Any:1194        """Build extra kwargs from additional params that were passed in."""1195        all_required_field_names = get_pydantic_field_names(cls)1196        return _build_model_kwargs(values, all_required_field_names)11971198    @model_validator(mode="before")1199    @classmethod1200    def _resolve_gateway(cls, values: Any) -> Any:1201        """Resolve the base URL and API key, applying LangSmith gateway settings.12021203        An explicit ``base_url``/``api_key`` always wins. Otherwise the base URL1204        falls back to ``FIREWORKS_API_BASE``, then the LangSmith gateway. The1205        gateway key is preferred only when the base URL came from the gateway;1206        for any other endpoint the provider key wins, and the gateway key is a1207        candidate only when the gateway is enabled.1208        """1209        if isinstance(values, dict):1210            config = _apply_gateway_config(1211                values,1212                cls,1213                base_url_field="fireworks_api_base",1214                api_key_field="fireworks_api_key",1215                provider_path="fireworks",1216                base_url_env="FIREWORKS_API_BASE",1217                api_key_env="FIREWORKS_API_KEY",1218            )1219            if config.api_key is None:1220                msg = (1221                    "You must specify an api key. "1222                    "You can pass it an argument as `api_key=...` or "1223                    "set the environment variable `FIREWORKS_API_KEY`."1224                )1225                raise ValueError(msg)1226        return values12271228    @model_validator(mode="after")1229    def _set_fireworks_chat_version(self) -> Self:1230        """Set package version in metadata."""1231        self._add_version("langchain-fireworks", __version__)1232        return self12331234    @model_validator(mode="after")1235    def validate_environment(self) -> Self:1236        """Validate that api key and python package exists in environment."""1237        if self.n < 1:1238            msg = "n must be at least 1."1239            raise ValueError(msg)1240        if self.n > 1 and self.streaming:1241            msg = "n must be 1 when streaming."1242            raise ValueError(msg)12431244        api_key = self.fireworks_api_key.get_secret_value()1245        base_url = self.fireworks_api_base1246        # 0.x accepted a `(connect, read)` tuple. 1.x's SDK only accepts a1247        # float, `httpx.Timeout`, or `None` — normalize so existing user code1248        # keeps working.1249        if isinstance(self.request_timeout, tuple):1250            connect, read = self.request_timeout1251            timeout: Any = httpx.Timeout(read, connect=connect)1252        else:1253            timeout = self.request_timeout1254        # `langchain-fireworks` owns retry/backoff via `_create_retry_decorator`1255        # so the LangChain `run_manager` sees each attempt. Suppress the1256        # SDK's built-in retry layer to avoid double-retrying.1257        if not self.client:1258            self._sdk_client = Fireworks(1259                api_key=api_key,1260                base_url=base_url,1261                timeout=timeout,1262                max_retries=0,1263            )1264            self.client = self._sdk_client.chat.completions1265        if not self.async_client:1266            self._async_sdk_client = AsyncFireworks(1267                api_key=api_key,1268                base_url=base_url,1269                timeout=timeout,1270                max_retries=0,1271            )1272            self.async_client = self._async_sdk_client.chat.completions1273        return self12741275    def close(self) -> None:1276        """Close the underlying sync HTTP client.12771278        After calling, sync invocations on this model will raise. Async1279        invocations remain available until `aclose()` is also called. Safe to1280        call multiple times.1281        """1282        if self._sdk_client is not None:1283            self._sdk_client.close()12841285    async def aclose(self) -> None:1286        """Close the underlying async HTTP client.12871288        Releases the aiohttp-backed connector that the 1.x SDK uses by1289        default. Without this, transient `ChatFireworks` instances can leak1290        an `Unclosed connector` warning at GC if the event loop has already1291        stopped. Safe to call multiple times.1292        """1293        if self._async_sdk_client is not None:1294            await self._async_sdk_client.close()12951296    def _resolve_model_profile(self) -> ModelProfile | None:1297        return _get_default_model_profile(self.model_name) or None12981299    @model_validator(mode="after")1300    def _set_default_max_tokens(self) -> Self:1301        """Set default `max_tokens` from the model profile when available."""1302        if self.max_tokens is None and self.profile:1303            self.max_tokens = self.profile.get("max_output_tokens")1304        return self13051306    @property1307    def _default_params(self) -> dict[str, Any]:1308        """Get the default parameters for calling Fireworks API."""1309        params = {1310            "model": self.model_name,1311            "stream": self.streaming,1312            "n": self.n,1313            "stop": self.stop,1314            **self.model_kwargs,1315        }1316        if self.temperature is not None:1317            params["temperature"] = self.temperature1318        if self.max_tokens is not None:1319            params["max_tokens"] = self.max_tokens1320        if self.service_tier is not None:1321            params["service_tier"] = self.service_tier1322        if self.reasoning_effort is not None:1323            params["reasoning_effort"] = self.reasoning_effort1324        return params13251326    def _get_ls_params(1327        self, stop: list[str] | None = None, **kwargs: Any1328    ) -> LangSmithParams:1329        """Get standard params for tracing."""1330        params = self._get_invocation_params(stop=stop, **kwargs)1331        ls_params = LangSmithParams(1332            ls_provider="fireworks",1333            ls_model_name=params.get("model", self.model_name),1334            ls_model_type="chat",1335            ls_temperature=params.get("temperature", self.temperature),1336        )1337        if ls_max_tokens := params.get("max_tokens", self.max_tokens):1338            ls_params["ls_max_tokens"] = ls_max_tokens1339        if ls_stop := stop or params.get("stop", None):1340            ls_params["ls_stop"] = ls_stop1341        return ls_params13421343    def _combine_llm_outputs(self, llm_outputs: list[dict | None]) -> dict:1344        overall_token_usage: dict = {}1345        system_fingerprint = None1346        model_name = None1347        for output in llm_outputs:1348            if output is None:1349                # Happens in streaming1350                continue1351            token_usage = output.get("token_usage")1352            if token_usage is not None:1353                for k, v in token_usage.items():1354                    if v is None:1355                        continue1356                    if k in overall_token_usage:1357                        overall_token_usage[k] = _update_token_usage(1358                            overall_token_usage[k], v1359                        )1360                    else:1361                        overall_token_usage[k] = v1362            if system_fingerprint is None:1363                system_fingerprint = output.get("system_fingerprint")1364            if model_name is None:1365                model_name = output.get("model_name") or None1366        combined = {1367            "token_usage": overall_token_usage,1368            "model_name": model_name or self.model_name,1369        }1370        if system_fingerprint:1371            combined["system_fingerprint"] = system_fingerprint1372        return combined13731374    def _usage_generation_chunk(self, usage: UsageMetadata) -> ChatGenerationChunk:1375        """Build the single chunk that carries a stream's token usage."""1376        message_chunk = AIMessageChunk(1377            content="",1378            usage_metadata=usage,1379            response_metadata={"model_provider": "fireworks"},1380        )1381        if self.output_version == "v1":1382            message_chunk = _format_chunk_for_v1(message_chunk)1383        return ChatGenerationChunk(message=message_chunk)13841385    def _stream(1386        self,1387        messages: list[BaseMessage],1388        stop: list[str] | None = None,1389        run_manager: CallbackManagerForLLMRun | None = None,1390        **kwargs: Any,1391    ) -> Iterator[ChatGenerationChunk]:1392        message_dicts, params = self._create_message_dicts(messages, stop)1393        params = {**params, **kwargs, "stream": True}1394        if self.stream_usage and "stream_options" not in params:1395            params["stream_options"] = {"include_usage": True}13961397        default_chunk_class: type[BaseMessageChunk] = AIMessageChunk1398        try:1399            stream = _completion_with_retry(1400                self, run_manager=run_manager, messages=message_dicts, **params1401            )1402        except BadRequestError as e:1403            _handle_fireworks_invalid_request(e)1404        except APIError as e:1405            _handle_fireworks_api_error(e)1406        response_model = None1407        last_usage: UsageMetadata | None = None1408        for chunk in stream:1409            if not isinstance(chunk, dict):1410                chunk = chunk.model_dump()1411            response_model = chunk.get("model") or response_model1412            message_chunk = _convert_chunk_to_message_chunk(chunk, default_chunk_class)1413            if self.output_version == "v1" and isinstance(1414                message_chunk, AIMessageChunk1415            ):1416                message_chunk = _format_chunk_for_v1(message_chunk)1417            if (1418                isinstance(message_chunk, AIMessageChunk)1419                and message_chunk.usage_metadata1420            ):1421                # Fireworks can report usage on more than one chunk (e.g. the1422                # `finish_reason` chunk and a trailing usage-only chunk). Merging1423                # chunks sums usage, so hold it back and emit the last report once.1424                last_usage = _record_usage(last_usage, message_chunk.usage_metadata)1425                message_chunk.usage_metadata = None1426                if not chunk.get("choices"):1427                    continue1428            generation_info: dict[str, Any] = {}1429            logprobs = None1430            if choices := chunk.get("choices"):1431                choice = choices[0]1432                if finish_reason := choice.get("finish_reason"):1433                    generation_info["finish_reason"] = finish_reason1434                    # Emit once: repeated model names concatenate when chunks merge.1435                    generation_info["model_name"] = response_model or self.model_name1436                    if service_tier := chunk.get("service_tier"):1437                        generation_info["service_tier"] = service_tier1438                logprobs = choice.get("logprobs")1439                if logprobs:1440                    generation_info["logprobs"] = logprobs1441            default_chunk_class = message_chunk.__class__1442            generation_chunk = ChatGenerationChunk(1443                message=message_chunk, generation_info=generation_info or None1444            )1445            if run_manager:1446                run_manager.on_llm_new_token(1447                    generation_chunk.text, chunk=generation_chunk, logprobs=logprobs1448                )1449            yield generation_chunk1450        if last_usage:1451            usage_chunk = self._usage_generation_chunk(last_usage)1452            if run_manager:1453                run_manager.on_llm_new_token("", chunk=usage_chunk)1454            yield usage_chunk14551456    def _generate(1457        self,1458        messages: list[BaseMessage],1459        stop: list[str] | None = None,1460        run_manager: CallbackManagerForLLMRun | None = None,1461        stream: bool | None = None,  # noqa: FBT0011462        **kwargs: Any,1463    ) -> ChatResult:1464        should_stream = stream if stream is not None else self.streaming1465        if should_stream:1466            stream_iter = self._stream(1467                messages, stop=stop, run_manager=run_manager, **kwargs1468            )1469            if self.output_version == "v1":1470                stream_iter = _finalize_stream(stream_iter)1471            return generate_from_stream(stream_iter)1472        message_dicts, params = self._create_message_dicts(messages, stop)1473        params = {1474            **params,1475            **({"stream": stream} if stream is not None else {}),1476            **kwargs,1477        }1478        try:1479            response = _completion_with_retry(1480                self, run_manager=run_manager, messages=message_dicts, **params1481            )1482        except BadRequestError as e:1483            _handle_fireworks_invalid_request(e)1484        except APIError as e:1485            _handle_fireworks_api_error(e)1486        return self._create_chat_result(response)14871488    def _create_message_dicts(1489        self, messages: list[BaseMessage], stop: list[str] | None1490    ) -> tuple[list[dict[str, Any]], dict[str, Any]]:1491        params = self._default_params1492        if stop is not None:1493            params["stop"] = stop1494        message_dicts = [_convert_message_to_dict(m) for m in messages]1495        return message_dicts, params14961497    def _create_chat_result(self, response: dict | BaseModel) -> ChatResult:1498        generations = []1499        if not isinstance(response, dict):1500            response = response.model_dump()1501        token_usage = response.get("usage", {})1502        model_name = response.get("model") or self.model_name1503        service_tier = response.get("service_tier")1504        for res in response["choices"]:1505            message = _convert_dict_to_message(res["message"])1506            if isinstance(message, AIMessage):1507                message.response_metadata["model_name"] = model_name1508                if token_usage:1509                    message.usage_metadata = _usage_to_metadata(token_usage)1510                    message.response_metadata["model_provider"] = "fireworks"1511                if service_tier:1512                    message.response_metadata["service_tier"] = service_tier1513            generation_info = {"finish_reason": res.get("finish_reason")}1514            if "logprobs" in res:1515                generation_info["logprobs"] = res["logprobs"]1516            gen = ChatGeneration(1517                message=message,1518                generation_info=generation_info,1519            )1520            generations.append(gen)1521        llm_output = {1522            "token_usage": token_usage,1523            "model_name": model_name,1524            "system_fingerprint": response.get("system_fingerprint", ""),1525        }1526        if service_tier:1527            llm_output["service_tier"] = service_tier1528        return ChatResult(generations=generations, llm_output=llm_output)15291530    async def _astream(1531        self,1532        messages: list[BaseMessage],1533        stop: list[str] | None = None,1534        run_manager: AsyncCallbackManagerForLLMRun | None = None,1535        **kwargs: Any,1536    ) -> AsyncIterator[ChatGenerationChunk]:1537        message_dicts, params = self._create_message_dicts(messages, stop)1538        params = {**params, **kwargs, "stream": True}1539        if self.stream_usage and "stream_options" not in params:1540            params["stream_options"] = {"include_usage": True}15411542        default_chunk_class: type[BaseMessageChunk] = AIMessageChunk1543        try:1544            stream = await _acompletion_with_retry(1545                self, run_manager=run_manager, messages=message_dicts, **params1546            )1547        except BadRequestError as e:1548            _handle_fireworks_invalid_request(e)1549        except APIError as e:1550            _handle_fireworks_api_error(e)1551        response_model = None1552        last_usage: UsageMetadata | None = None1553        async for chunk in stream:1554            if not isinstance(chunk, dict):1555                chunk = chunk.model_dump()1556            response_model = chunk.get("model") or response_model1557            message_chunk = _convert_chunk_to_message_chunk(chunk, default_chunk_class)1558            if self.output_version == "v1" and isinstance(1559                message_chunk, AIMessageChunk1560            ):1561                message_chunk = _format_chunk_for_v1(message_chunk)1562            if (1563                isinstance(message_chunk, AIMessageChunk)1564                and message_chunk.usage_metadata1565            ):1566                # Fireworks can report usage on more than one chunk (e.g. the1567                # `finish_reason` chunk and a trailing usage-only chunk). Merging1568                # chunks sums usage, so hold it back and emit the last report once.1569                last_usage = _record_usage(last_usage, message_chunk.usage_metadata)1570                message_chunk.usage_metadata = None1571                if not chunk.get("choices"):1572                    continue1573            generation_info: dict[str, Any] = {}1574            logprobs = None1575            if choices := chunk.get("choices"):1576                choice = choices[0]1577                if finish_reason := choice.get("finish_reason"):1578                    generation_info["finish_reason"] = finish_reason1579                    # Emit once: repeated model names concatenate when chunks merge.1580                    generation_info["model_name"] = response_model or self.model_name1581                    if service_tier := chunk.get("service_tier"):1582                        generation_info["service_tier"] = service_tier1583                logprobs = choice.get("logprobs")1584                if logprobs:1585                    generation_info["logprobs"] = logprobs1586            default_chunk_class = message_chunk.__class__1587            generation_chunk = ChatGenerationChunk(1588                message=message_chunk, generation_info=generation_info or None1589            )1590            if run_manager:1591                await run_manager.on_llm_new_token(1592                    token=generation_chunk.text,1593                    chunk=generation_chunk,1594                    logprobs=logprobs,1595                )1596            yield generation_chunk1597        if last_usage:1598            usage_chunk = self._usage_generation_chunk(last_usage)1599            if run_manager:1600                await run_manager.on_llm_new_token("", chunk=usage_chunk)1601            yield usage_chunk16021603    async def _agenerate(1604        self,1605        messages: list[BaseMessage],1606        stop: list[str] | None = None,1607        run_manager: AsyncCallbackManagerForLLMRun | None = None,1608        stream: bool | None = None,  # noqa: FBT0011609        **kwargs: Any,1610    ) -> ChatResult:1611        should_stream = stream if stream is not None else self.streaming1612        if should_stream:1613            stream_iter = self._astream(1614                messages, stop=stop, run_manager=run_manager, **kwargs1615            )1616            if self.output_version == "v1":1617                stream_iter = _afinalize_stream(stream_iter)1618            return await agenerate_from_stream(stream_iter)16191620        message_dicts, params = self._create_message_dicts(messages, stop)1621        params = {1622            **params,1623            **({"stream": stream} if stream is not None else {}),1624            **kwargs,1625        }1626        try:1627            response = await _acompletion_with_retry(1628                self, run_manager=run_manager, messages=message_dicts, **params1629            )1630        except BadRequestError as e:1631            _handle_fireworks_invalid_request(e)1632        except APIError as e:1633            _handle_fireworks_api_error(e)1634        return self._create_chat_result(response)16351636    @property1637    def _identifying_params(self) -> dict[str, Any]:1638        """Get the identifying parameters."""1639        return {"model_name": self.model_name, **self._default_params}16401641    def _get_invocation_params(1642        self, stop: list[str] | None = None, **kwargs: Any1643    ) -> dict[str, Any]:1644        """Get the parameters used to invoke the model."""1645        return {1646            "model": self.model_name,1647            **super()._get_invocation_params(stop=stop),1648            **self._default_params,1649            **kwargs,1650        }16511652    @property1653    def _llm_type(self) -> str:1654        """Return type of chat model."""1655        return "fireworks-chat"16561657    def bind_tools(1658        self,1659        tools: Sequence[dict[str, Any] | type[BaseModel] | Callable | BaseTool],1660        *,1661        tool_choice: dict | str | bool | None = None,1662        **kwargs: Any,1663    ) -> Runnable[LanguageModelInput, AIMessage]:1664        """Bind tool-like objects to this chat model.16651666        Assumes model is compatible with Fireworks tool-calling API.16671668        Args:1669            tools: A list of tool definitions to bind to this chat model.16701671                Supports any tool definition handled by [`convert_to_openai_tool`][langchain_core.utils.function_calling.convert_to_openai_tool].1672            tool_choice: Which tool to require the model to call.1673                Must be the name of the single provided function,1674                `'auto'` to automatically determine which function to call1675                with the option to not call any function, `'any'` to enforce that some1676                function is called, or a dict of the form:1677                `{"type": "function", "function": {"name": <<tool_name>>}}`.1678            **kwargs: Any additional parameters to pass to1679                `langchain_fireworks.chat_models.ChatFireworks.bind`1680        """  # noqa: E5011681        strict = kwargs.pop("strict", None)1682        formatted_tools = [1683            convert_to_openai_tool(tool, strict=strict) for tool in tools1684        ]1685        if tool_choice is not None and tool_choice:1686            if isinstance(tool_choice, str) and (1687                tool_choice not in ("auto", "any", "none")1688            ):1689                tool_choice = {"type": "function", "function": {"name": tool_choice}}1690            if isinstance(tool_choice, bool):1691                if len(tools) > 1:1692                    msg = (1693                        "tool_choice can only be True when there is one tool. Received "1694                        f"{len(tools)} tools."1695                    )1696                    raise ValueError(msg)1697                tool_name = formatted_tools[0]["function"]["name"]1698                tool_choice = {1699                    "type": "function",1700                    "function": {"name": tool_name},1701                }17021703            kwargs["tool_choice"] = tool_choice1704        return super().bind(tools=formatted_tools, **kwargs)17051706    def with_structured_output(1707        self,1708        schema: dict | type[BaseModel] | None = None,1709        *,1710        method: Literal[1711            "function_calling", "json_mode", "json_schema"1712        ] = "function_calling",1713        include_raw: bool = False,1714        **kwargs: Any,1715    ) -> Runnable[LanguageModelInput, dict | BaseModel]:1716        """Model wrapper that returns outputs formatted to match the given schema.17171718        Args:1719            schema: The output schema. Can be passed in as:17201721                - An OpenAI function/tool schema,1722                - A JSON Schema,1723                - A `TypedDict` class,1724                - Or a Pydantic class.17251726                If `schema` is a Pydantic class then the model output will be a1727                Pydantic instance of that class, and the model-generated fields will be1728                validated by the Pydantic class. Otherwise the model output will be a1729                dict and will not be validated.17301731                See `langchain_core.utils.function_calling.convert_to_openai_tool` for1732                more on how to properly specify types and descriptions of schema fields1733                when specifying a Pydantic or `TypedDict` class.17341735            method: The method for steering model generation, one of:17361737                - `'function_calling'`:1738                    Uses Fireworks's [tool-calling features](https://docs.fireworks.ai/guides/function-calling).1739                - `'json_schema'`:1740                    Uses Fireworks's [structured output feature](https://docs.fireworks.ai/structured-responses/structured-response-formatting).1741                - `'json_mode'`:1742                    Uses Fireworks's [JSON mode feature](https://docs.fireworks.ai/structured-responses/structured-response-formatting).17431744                !!! warning "Behavior changed in `langchain-fireworks` 0.2.8"17451746                    Added support for `'json_schema'`.17471748            include_raw:1749                If `False` then only the parsed structured output is returned.17501751                If an error occurs during model output parsing it will be raised.17521753                If `True` then both the raw model response (a `BaseMessage`) and the1754                parsed model response will be returned.17551756                If an error occurs during output parsing it will be caught and returned1757                as well.17581759                The final output is always a `dict` with keys `'raw'`, `'parsed'`, and1760                `'parsing_error'`.17611762            kwargs:1763                Any additional parameters to pass to the `langchain.runnable.Runnable`1764                constructor.17651766        Returns:1767            A `Runnable` that takes same inputs as a1768                `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is1769                `False` and `schema` is a Pydantic class, `Runnable` outputs an instance1770                of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is1771                `False` then `Runnable` outputs a `dict`.17721773                If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:17741775                - `'raw'`: `BaseMessage`1776                - `'parsed'`: `None` if there was a parsing error, otherwise the type1777                    depends on the `schema` as described above.1778                - `'parsing_error'`: `BaseException | None`17791780        Example: schema=Pydantic class, method="function_calling", include_raw=False:17811782        ```python1783        from typing import Optional17841785        from langchain_fireworks import ChatFireworks1786        from pydantic import BaseModel, Field178717881789        class AnswerWithJustification(BaseModel):1790            '''An answer to the user question along with justification for the answer.'''17911792            answer: str1793            # If we provide default values and/or descriptions for fields, these will be passed1794            # to the model. This is an important part of improving a model's ability to1795            # correctly return structured outputs.1796            justification: str | None = Field(1797                default=None, description="A justification for the answer."1798            )179918001801        model = ChatFireworks(1802            model="accounts/fireworks/models/gpt-oss-120b",1803            temperature=0,1804        )1805        structured_model = model.with_structured_output(AnswerWithJustification)18061807        structured_model.invoke(1808            "What weighs more a pound of bricks or a pound of feathers"1809        )18101811        # -> AnswerWithJustification(1812        #     answer='They weigh the same',1813        #     justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'1814        # )1815        ```18161817        Example: schema=Pydantic class, method="function_calling", include_raw=True:18181819        ```python1820        from langchain_fireworks import ChatFireworks1821        from pydantic import BaseModel182218231824        class AnswerWithJustification(BaseModel):1825            '''An answer to the user question along with justification for the answer.'''18261827            answer: str1828            justification: str182918301831        model = ChatFireworks(1832            model="accounts/fireworks/models/gpt-oss-120b",1833            temperature=0,1834        )1835        structured_model = model.with_structured_output(1836            AnswerWithJustification, include_raw=True1837        )18381839        structured_model.invoke(1840            "What weighs more a pound of bricks or a pound of feathers"1841        )1842        # -> {1843        #     'raw': AIMessage(content='', additional_kwargs={'tool_calls': [{'id': 'call_Ao02pnFYXD6GN1yzc0uXPsvF', 'function': {'arguments': '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}', 'name': 'AnswerWithJustification'}, 'type': 'function'}]}),1844        #     'parsed': AnswerWithJustification(answer='They weigh the same.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.'),1845        #     'parsing_error': None1846        # }1847        ```18481849        Example: schema=TypedDict class, method="function_calling", include_raw=False:18501851        ```python1852        from typing_extensions import Annotated, TypedDict18531854        from langchain_fireworks import ChatFireworks185518561857        class AnswerWithJustification(TypedDict):1858            '''An answer to the user question along with justification for the answer.'''18591860            answer: str1861            justification: Annotated[1862                str | None, None, "A justification for the answer."1863            ]186418651866        model = ChatFireworks(1867            model="accounts/fireworks/models/gpt-oss-120b",1868            temperature=0,1869        )1870        structured_model = model.with_structured_output(AnswerWithJustification)18711872        structured_model.invoke(1873            "What weighs more a pound of bricks or a pound of feathers"1874        )1875        # -> {1876        #     'answer': 'They weigh the same',1877        #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'1878        # }1879        ```18801881        Example: schema=OpenAI function schema, method="function_calling", include_raw=False:18821883        ```python1884        from langchain_fireworks import ChatFireworks18851886        oai_schema = {1887            "name": "AnswerWithJustification",1888            "description": "An answer to the user question along with justification for the answer.",1889            "parameters": {1890                "type": "object",1891                "properties": {1892                    "answer": {"type": "string"},1893                    "justification": {1894                        "description": "A justification for the answer.",1895                        "type": "string",1896                    },1897                },1898                "required": ["answer"],1899            },1900        }19011902        model = ChatFireworks(1903            model="accounts/fireworks/models/gpt-oss-120b",1904            temperature=0,1905        )1906        structured_model = model.with_structured_output(oai_schema)19071908        structured_model.invoke(1909            "What weighs more a pound of bricks or a pound of feathers"1910        )1911        # -> {1912        #     'answer': 'They weigh the same',1913        #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'1914        # }1915        ```19161917        Example: schema=Pydantic class, method="json_mode", include_raw=True:19181919        ```python1920        from langchain_fireworks import ChatFireworks1921        from pydantic import BaseModel192219231924        class AnswerWithJustification(BaseModel):1925            answer: str1926            justification: str192719281929        model = ChatFireworks(1930            model="accounts/fireworks/models/gpt-oss-120b", temperature=01931        )1932        structured_model = model.with_structured_output(1933            AnswerWithJustification, method="json_mode", include_raw=True1934        )19351936        structured_model.invoke(1937            "Answer the following question. "1938            "Make sure to return a JSON blob with keys 'answer' and 'justification'. "1939            "What's heavier a pound of bricks or a pound of feathers?"1940        )1941        # -> {1942        #     'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'),1943        #     'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'),1944        #     'parsing_error': None1945        # }1946        ```19471948        Example: schema=None, method="json_mode", include_raw=True:19491950        ```python1951        structured_model = model.with_structured_output(1952            method="json_mode", include_raw=True1953        )19541955        structured_model.invoke(1956            "Answer the following question. "1957            "Make sure to return a JSON blob with keys 'answer' and 'justification'. "1958            "What's heavier a pound of bricks or a pound of feathers?"1959        )1960        # -> {1961        #     'raw': AIMessage(content='{"answer": "They are both the same weight.", "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight."}'),1962        #     'parsed': {1963        #         'answer': 'They are both the same weight.',1964        #         'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'1965        #     },1966        #     'parsing_error': None1967        # }1968        ```19691970        """  # noqa: E5011971        _ = kwargs.pop("strict", None)1972        if kwargs:1973            msg = f"Received unsupported arguments {kwargs}"1974            raise ValueError(msg)1975        is_pydantic_schema = _is_pydantic_class(schema)1976        if method == "function_calling":1977            if schema is None:1978                msg = (1979                    "schema must be specified when method is 'function_calling'. "1980                    "Received None."1981                )1982                raise ValueError(msg)1983            formatted_tool = convert_to_openai_tool(schema)1984            tool_name = formatted_tool["function"]["name"]1985            llm = self.bind_tools(1986                [schema],1987                tool_choice=tool_name,1988                ls_structured_output_format={1989                    "kwargs": {"method": "function_calling"},1990                    "schema": formatted_tool,1991                },1992            )1993            if is_pydantic_schema:1994                output_parser: OutputParserLike = PydanticToolsParser(1995                    tools=[schema],  # type: ignore[list-item]1996                    first_tool_only=True,  # type: ignore[list-item]1997                )1998            else:1999                output_parser = JsonOutputKeyToolsParser(2000                    key_name=tool_name, first_tool_only=True

Code quality findings 2

Ensure functions have docstrings for documentation
info maintainability missing-docstring
def lc_secrets(self) -> dict[str, str]:
Ensure functions have docstrings for documentation
info maintainability missing-docstring
def lc_attributes(self) -> dict[str, Any]:

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings