Support per-stream hotwords / context for FunASR Nano (offline) via OfflineStream::SetOption
Use case
We build an on-device dictation tool. In our local pipeline a long utterance is split into segments that are decoded in parallel by the FunASR Nano offline recognizer while the user is still speaking. We would like to feed the text decoded so far as context for the following segments, so that terminology stays consistent across segment boundaries.
This looks like exactly what the model is designed for: the released model ships
rag_math.wav / rag_physics.wav / rag_chemistry.wav test files, and the official
funasr Python API takes hotwords= as a per-call argument of model.generate(...).
What already works (no change needed)
With hotwords empty, language empty and itn = true, BuildUserPrompt()
(sherpa-onnx/csrc/offline-recognizer-funasr-nano-impl.cc) returns the caller-supplied
user_prompt verbatim, so an application can assemble the official context block itself:
请结合上下文信息,更加准确地完成语音转写任务。如果没有相关信息,我们会留空。
**上下文信息:**
热词列表:[...]
前文:...
语音转写:We verified this works: on rag_chemistry.wav, the baseline output contains 「脂」, and after
injecting a context line containing 「酯」 the output is corrected to 「酯」. We also verified
that a self-assembled prompt with the same content is byte-identical to the one built
internally, so the mechanism is confirmed rather than inferred.
The problem
user_prompt and hotwords live in OfflineFunASRNanoModelConfig, i.e. they are fixed at
recognizer construction. Changing the context per utterance therefore requires recreating
the recognizer, which reloads ~1 GB of ONNX weights (several seconds). That makes per-utterance
context impractical, even though the model itself supports it.
Two existing mechanisms do not help:
OfflineRecognizerImpl::CreateStream(const std::string &hotwords)— the base implementation isSHERPA_ONNX_LOGE("Only transducer models support contextual biasing."); SHERPA_ONNX_EXIT(-1);OfflineRecognizerFunASRNanoImpldoes not override it, so calling it does not merely ignore the argument — it terminates the process. For a desktop application that is a hard crash with no recoverable error, so we had to make sure never to call it.OfflineRecognizerImpl::SetConfig()assigns to the base classconfig_, whileOfflineRecognizerFunASRNanoImplkeeps its ownconfig_member and reads that one inGenerateText(). SoSetConfig()would silently have no effect for this recognizer.
Proposal
OfflineStream already provides a generic per-stream option mechanism
(sherpa-onnx/csrc/offline-stream.h):
// Generic per-stream option mechanism (key-value string pairs).
void SetOption(const std::string &key, const std::string &value);
bool HasOption(const std::string &key) const;
const std::string &GetOption(const std::string &key) const;As far as we can tell no offline recognizer consumes it yet (we checked funasr-nano, qwen3-asr, whisper and canary in v1.12.38 and funasr-nano on master).
It would be very useful if OfflineRecognizerFunASRNanoImpl::DecodeStreams() read a few
per-stream options and let them override the config values before calling BuildUserPrompt(),
for example:
hotwordsuser_prompt- possibly
language/itn
That would allow per-utterance context with no model reload, and it keeps the existing construction-time defaults for callers that do not set any option.
Separately, it may be worth overriding CreateStream(const std::string &hotwords) in the
recognizers that do not support it so that it logs and falls back instead of calling
SHERPA_ONNX_EXIT(-1) — a library terminating the host process is surprising for
application embedders.
Environment
- sherpa-onnx
v1.12.38(Rust bindings, prebuilt shared library); source ofmasteralso checked - Model:
sherpa-onnx-funasr-nano-int8-2025-12-30 - Platform: Windows 11 x64, CPU provider
Thanks a lot for adding hotwords / itn support in #3092 — it is working well for us; this
issue is only about making the same information settable per utterance.
Source: k2-fsa/sherpa-onnx