Issue with Langchain: Error When Implementing ConstitutionalChain with RetrievalQaChain and Vecatra
Labels: name: area: agent, name: auto:bug

### System Info

I am currently working with the Langchain platform and I've encountered an issue during the integration of ConstitutionalChain with the existing retrievalQaChain.

In my implementation, I've used retrievalQaChain with a custom prompt and Vecatra as the retriever. My system is hosted on Vercel and I'm primarily aiming to acquire a streaming output.

My objective was to utilize ConstitutionalChain alongside retrievalQaChain to improve the system's overall performance. However, upon integrating ConstitutionalChain, I am continually facing an error that disrupts the seamless functioning of my system.

Regrettably, I've not been able to identify the root cause or find a solution for this persistent issue. It would be highly beneficial if I could receive assistance to overcome this obstacle. To provide better context and aid the debugging process, I'll follow up with the specifics of the error message, the steps that lead to the error, and any relevant screenshots if possible.

 **Error: error [TypeError: Cannot read properties of undefined (reading 'format')]**

### Who can help?

_No response_

### Information

- [ ] The official example notebooks/scripts
- [ ] My own modified scripts

### Related Components

- [ ] LLMs/Chat Models
- [ ] Embedding Models
- [ ] Prompts / Prompt Templates / Prompt Selectors
- [ ] Output Parsers
- [ ] Document Loaders
- [ ] Vector Stores / Retrievers
- [ ] Memory
- [ ] Agents / Agent Executors
- [ ] Tools / Toolkits
- [X] Chains
- [ ] Callbacks/Tracing
- [ ] Async

### Reproduction

const model = new ChatOpenAI({
      temperature: 0,
      azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION,
      azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY,
      azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME,
      azureOpenAIApiDeploymentName:
        process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME,
      streaming: true,
      callbackManager: CallbackManager.fromHandlers(handlers),
    });
    const metadataFilter = `doc.sourceId=${SourceId}`;
    const retrivalChain = new RetrievalQAChain({
      combineDocumentsChain: loadQAStuffChain(model, { prompt }),
      retriever: vectorStore.asRetriever(10, { filter: metadataFilter }),
      returnSourceDocuments: true,
    });
    const principle = new ConstitutionalPrinciple({
      name: 'Ethical Principle',
      critiqueRequest:
        'The model should only talk about ethical and legal things.',
      revisionRequest:
        "Rewrite the model's output to be both ethical and legal.",
    });
    const chain = ConstitutionalChain.fromLLM(model, {
      chain: retrivalChain,
      constitutionalPrinciples: [principle],
    });
    chain
      .call({
        query,
      })
      .then((result) => {
        // TODO: consume the references
        // eslint-disable-next-line no-console
        console.log('result.sourceDocuments', result.sourceDocuments); // This will log the source documents
      })
      .catch((e) => {
        // eslint-disable-next-line no-console
        console.error('error', e.message);
      })
      .finally(() => {
        handlers.handleChainEnd();
      });
    const corsHeaders = {
      'Access-Control-Allow-Origin': '*',
    };
    return new StreamingTextResponse(stream, {
      headers: corsHeaders,
    });
   

### Expected behavior
It should return the streamed output with the constitutional principle